cta*_*cke 12 c# database reporting
由于存在降级的风险,我想问一下,使用C#和大量列来查看表中的数据的最佳机制(最好对于此处固有的实践违规显然是主观的).很多,我的意思是1000.
现在,在你点击所有点击开心之前,或者抛出一些回答,比如"你为什么会有一个包含那么多列的表",让我说它实际上是设计要求的一部分.我们正在从1000个数据点尽快收集数据.我们需要尽可能快地存储这些,因此平台.数据需要可以直接从SQL Server访问,因此数据库(我们使用带有table-direct的SQL Compact).
所以,让我们暂时忘记我们所学到的关于正确数据库设计,规范化规则等的所有内容,并且只关注我有一个包含1000列的表的事实,并且我希望能够显示数据屏幕以验证数据是否实际进入那里.
我试过一个数据网格.它呕吐是因为(毫不奇怪)它不是为处理那么多列而设计的.
我试过在Studio中使用查看器.它在256之后呕吐,加上最终用户无论如何都不会安装Studio.
目前,结果不一定非常好,它不需要是可更新的,也不需要对数据更改敏感 - 只是在给定时间点表中数据的静态快照.
相关(或半相关)信息:
con*_*tor 14
您可以将所有数字格式化为带有空格的n字符字符串,然后以固定宽度字体显示它们.
1 2 3 4 6 36 436 6346
2 3 4 6 36 436 6346 0
3 4 6 36 436 6346 3 4
4 6 36 436 6346 333 222 334
Run Code Online (Sandbox Code Playgroud)
cta*_*cke 12
好吧,对我来说最合适的答案是使用ReportViewer控件,但不能以MSDN中记录的任何方式使用.问题是我有动态数据,因此我需要一个动态报告,并且所有教程等似乎都假设您可以在设计时了解所有内容,因此您可以通过向导指向并单击您的方式.
解决方案最终需要几件.首先,我必须创建代码来动态生成RDLC,ReportViewer用它来描述报告布局以及哪些数据字段映射到什么.这就是我想出的:
public static Stream BuildRDLCStream(
DataSet data, string name, string reportXslPath)
{
using (MemoryStream schemaStream = new MemoryStream())
{
// save the schema to a stream
data.WriteXmlSchema(schemaStream);
schemaStream.Seek(0, SeekOrigin.Begin);
// load it into a Document and set the Name variable
XmlDocument xmlDomSchema = new XmlDocument();
xmlDomSchema.Load(schemaStream);
xmlDomSchema.DocumentElement.SetAttribute("Name", data.DataSetName);
// load the report's XSL file (that's the magic)
XslCompiledTransform xform = new XslCompiledTransform();
xform.Load(reportXslPath);
// do the transform
MemoryStream rdlcStream = new MemoryStream();
XmlWriter writer = XmlWriter.Create(rdlcStream);
xform.Transform(xmlDomSchema, writer);
writer.Close();
rdlcStream.Seek(0, SeekOrigin.Begin);
// send back the RDLC
return rdlcStream;
}
}
Run Code Online (Sandbox Code Playgroud)
第二部分是我从Dan Shipe的博客中获取的 XSL文件.那里的RDLC代码非常没用,因为它全部用于Web使用,但XSL是纯金.我已经把它放在这篇文章的底部,以便在博客离线时完整.
一旦我拥有这两个部分,只需创建一个带有ReportViewer控件的Form,然后使用这段代码进行设置:
ds.DataSetName = name;
Stream rdlc = RdlcEngine.BuildRDLCStream(
ds, name, "c:\\temp\\rdlc\\report.xsl");
reportView.LocalReport.LoadReportDefinition(rdlc);
reportView.LocalReport.DataSources.Clear();
reportView.LocalReport.DataSources.Add(
new ReportDataSource(ds.DataSetName, ds.Tables[0]));
reportView.RefreshReport();
Run Code Online (Sandbox Code Playgroud)
这里的关键是'ds'是一个DataSet对象,其中包含一个DataTable,其中包含要显示的数据.
同样,为了完整性,这里是XSL - 抱歉大小:
<?xml version="1.0"?>
<!-- Stylesheet for creating ReportViewer RDLC documents -->
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:msxsl="urn:schemas-microsoft-com:xslt"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:msdata="urn:schemas-microsoft-com:xml-msdata"
xmlns:rd="http://schemas.microsoft.com/SQLServer/reporting/reportdesigner" xmlns="http://schemas.microsoft.com/sqlserver/reporting/2005/01/reportdefinition"
>
<xsl:variable name="mvarName" select="/xs:schema/@Name"/>
<xsl:variable name="mvarFontSize">8pt</xsl:variable>
<xsl:variable name="mvarFontWeight">500</xsl:variable>
<xsl:variable name="mvarFontWeightBold">700</xsl:variable>
<xsl:template match="/">
<xsl:apply-templates select="/xs:schema/xs:element/xs:complexType/xs:choice/xs:element/xs:complexType/xs:sequence">
</xsl:apply-templates>
</xsl:template>
<xsl:template match="xs:sequence">
<Report xmlns:rd="http://schemas.microsoft.com/SQLServer/reporting/reportdesigner" xmlns="http://schemas.microsoft.com/sqlserver/reporting/2005/01/reportdefinition">
<BottomMargin>1in</BottomMargin>
<RightMargin>1in</RightMargin>
<LeftMargin>1in</LeftMargin>
<TopMargin>1in</TopMargin>
<InteractiveHeight>11in</InteractiveHeight>
<InteractiveWidth>8.5in</InteractiveWidth>
<Width>6.5in</Width>
<Language>en-US</Language>
<rd:DrawGrid>true</rd:DrawGrid>
<rd:SnapToGrid>true</rd:SnapToGrid>
<rd:ReportID>7358b654-3ca3-44a0-8677-efe0a55c7c45</rd:ReportID>
<xsl:call-template name="BuildDataSource">
</xsl:call-template>
<xsl:call-template name="BuildDataSet">
</xsl:call-template>
<Body>
<Height>0.50in</Height>
<ReportItems>
<Table Name="table1">
<DataSetName><xsl:value-of select="$mvarName" /></DataSetName>
<Top>0.5in</Top>
<Height>0.50in</Height>
<Header>
<TableRows>
<TableRow>
<Height>0.25in</Height>
<TableCells>
<xsl:apply-templates select="xs:element" mode="HeaderTableCell">
</xsl:apply-templates>
</TableCells>
</TableRow>
</TableRows>
</Header>
<Details>
<TableRows>
<TableRow>
<Height>0.25in</Height>
<TableCells>
<xsl:apply-templates select="xs:element" mode="DetailTableCell">
</xsl:apply-templates>
</TableCells>
</TableRow>
</TableRows>
</Details>
<TableColumns>
<xsl:apply-templates select="xs:element" mode="TableColumn">
</xsl:apply-templates>
</TableColumns>
</Table>
</ReportItems>
</Body>
</Report>
</xsl:template>
<xsl:template name="BuildDataSource">
<DataSources>
<DataSource Name="DummyDataSource">
<ConnectionProperties>
<ConnectString/>
<DataProvider>SQL</DataProvider>
</ConnectionProperties>
<rd:DataSourceID>84635ff8-d177-4a25-9aa5-5a921652c79c</rd:DataSourceID>
</DataSource>
</DataSources>
</xsl:template>
<xsl:template name="BuildDataSet">
<DataSets>
<DataSet Name="{$mvarName}">
<Query>
<rd:UseGenericDesigner>true</rd:UseGenericDesigner>
<CommandText/>
<DataSourceName>DummyDataSource</DataSourceName>
</Query>
<Fields>
<xsl:apply-templates select="xs:element" mode="Field">
</xsl:apply-templates>
</Fields>
</DataSet>
</DataSets>
</xsl:template>
<xsl:template match="xs:element" mode="Field">
<xsl:variable name="varFieldName">
<xsl:value-of select="@name" />
</xsl:variable>
<xsl:variable name="varDataType">
<xsl:choose>
<xsl:when test="@type='xs:int'">System.Int32</xsl:when>
<xsl:when test="@type='xs:string'">System.String</xsl:when>
<xsl:when test="@type='xs:dateTime'">System.DateTime</xsl:when>
<xsl:when test="@type='xs:boolean'">System.Boolean</xsl:when>
</xsl:choose>
</xsl:variable>
<Field Name="{$varFieldName}">
<rd:TypeName><xsl:value-of select="$varDataType"/></rd:TypeName>
<DataField><xsl:value-of select="$varFieldName"/></DataField>
</Field>
</xsl:template>
<xsl:template match="xs:element" mode="HeaderTableCell">
<xsl:variable name="varFieldName">
<xsl:value-of select="@name" />
</xsl:variable>
<TableCell>
<ReportItems>
<Textbox Name="textbox{position()}">
<rd:DefaultName>textbox<xsl:value-of select="position()"/>
</rd:DefaultName>
<Value><xsl:value-of select="$varFieldName"/></Value>
<CanGrow>true</CanGrow>
<ZIndex>7</ZIndex>
<Style>
<TextAlign>Center</TextAlign>
<PaddingLeft>2pt</PaddingLeft>
<PaddingBottom>2pt</PaddingBottom>
<PaddingRight>2pt</PaddingRight>
<PaddingTop>2pt</PaddingTop>
<FontSize><xsl:value-of select="$mvarFontSize"/></FontSize>
<FontWeight><xsl:value-of select="$mvarFontWeightBold"/></FontWeight>
<BackgroundColor>#000000</BackgroundColor>
<Color>#ffffff</Color>
<BorderColor>
<Default>#ffffff</Default>
</BorderColor>
<BorderStyle>
<Default>Solid</Default>
</BorderStyle>
</Style>
</Textbox>
</ReportItems>
</TableCell>
</xsl:template>
<xsl:template match="xs:element" mode="DetailTableCell">
<xsl:variable name="varFieldName">
<xsl:value-of select="@name" />
</xsl:variable>
<TableCell>
<ReportItems>
<Textbox Name="{$varFieldName}">
<rd:DefaultName><xsl:value-of select="$varFieldName"/></rd:DefaultName>
<Value>=Fields!<xsl:value-of select="$varFieldName"/>.Value</Value>
<CanGrow>true</CanGrow>
<ZIndex>7</ZIndex>
<Style>
<TextAlign>Left</TextAlign>
<PaddingLeft>2pt</PaddingLeft>
<PaddingBottom>2pt</PaddingBottom>
<PaddingRight>2pt</PaddingRight>
<PaddingTop>2pt</PaddingTop>
<FontSize><xsl:value-of select="$mvarFontSize"/></FontSize>
<FontWeight><xsl:value-of select="$mvarFontWeight"/></FontWeight>
<BackgroundColor>#e0e0e0</BackgroundColor>
<Color>#000000</Color>
<BorderColor>
<Default>#ffffff</Default>
</BorderColor>
<BorderStyle>
<Default>Solid</Default>
</BorderStyle>
</Style>
</Textbox>
</ReportItems>
</TableCell>
</xsl:template>
<xsl:template match="xs:element" mode="TableColumn">
<TableColumn>
<Width>0.75in</Width>
</TableColumn>
</xsl:template>
<xsl:template name="replace-string">
<xsl:param name="text"/>
<xsl:param name="from"/>
<xsl:param name="to"/>
<xsl:choose>
<xsl:when test="contains($text, $from)">
<xsl:variable name="before" select="substring-before($text, $from)"/>
<xsl:variable name="after" select="substring-after($text, $from)"/>
<xsl:variable name="prefix" select="concat($before, $to)"/>
<xsl:value-of select="$before"/>
<xsl:value-of select="$to"/>
<xsl:call-template name="replace-string">
<xsl:with-param name="text" select="$after"/>
<xsl:with-param name="from" select="$from"/>
<xsl:with-param name="to" select="$to"/>
</xsl:call-template>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="$text"/>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
</xsl:stylesheet>
Run Code Online (Sandbox Code Playgroud)
如何将数据存储在csv文件中,这将为您提供查看选项.如果您的用户有excel或Open Office Calc,他们可以轻松导入数据(不确定Calc是否有列限制,但excel 2007可以容纳16384列)并通过该程序查看它?