如何修改报表数据源(连接字符串或SQL查询)取决于您使用的报表类型。本文主要讲解了如何修改RDL报表数据源。
页面/RDL报表
运行时在页面或RDL报表中新建一个数据源,即在代码中设置,但通常最好在设计时设置数据源,然后在运行时用代码修改他们。设计时设置数据,您可以拖动字段放入报表填充你的字段列表,这样就可以避免很多手工绑定。
下面的代码修改设计时数据源和数据集的连接字符串和SQL查询..
// C#
private void Form1_Load(…)
{
// Instantiate the report.
GrapeCity.ActiveReports.PageReport rpt = new GrapeCity.ActiveReports.PageReport();
// Load a report definition file.
rpt.Load(new System.IO.FileInfo(“PageReport1.rdlx”));
// Modify the connection string.
rpt.Report.DataSources[0].ConnectionProperties.DataProvider = “OLEDB”;
rpt.Report.DataSources[0].ConnectionProperties.ConnectString =
@”Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:/Reels.mdb”;
// Define the SQL string to use.
String tmpQuery = “Select Top 10 * From Movie”;
// Modify the SQL string.
rpt.Report.DataSets[0].Query.CommandText =
GrapeCity.ActiveReports.Expressions.ExpressionInfo.Parse(tmpQuery,
GrapeCity.ActiveReports.Expressions.ExpressionResultType.String);
GrapeCity.ActiveReports.Document.PageDocument pageDocument =
new GrapeCity.ActiveReports.Document.PageDocument(rpt);
viewer1.LoadDocument(pageDocument);
}
‘ VB.NET
Private Sub Form1_Load(…) Handles MyBase.Load
’ Instantiate the report.
Dim rpt As New GrapeCity.ActiveReports.PageReport()
’ Load a report definition file.
rpt.Load(New System.IO.FileInfo(“PageReport1.rdlx”))
’ Modify the connection string.
rpt.Report.DataSources(0).ConnectionProperties.DataProvider = “OLEDB”
rpt.Report.DataSources(0).ConnectionProperties.ConnectString _
= “Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:/Reels.mdb”
’ Define the SQL string to use.
Dim tmpQuery As [String] = “Select Top 10 * From Movie”
’ Modify the SQL string.
rpt.Report.DataSets(0).Query.CommandText = _
GrapeCity.ActiveReports.Expressions.ExpressionInfo.Parse(tmpQuery, _
GrapeCity.ActiveReports.Expressions.ExpressionResultType.[String])
Dim pageDocument As New GrapeCity.ActiveReports.Document.PageDocument(rpt)
Viewer1.LoadDocument(pageDocument)
End Sub
重要提示:当您使用一个含有多个数据集的RDL报表时,你可能需要将数据集索引设置为0以外的东西,您可以用像DataSets(x).Name的代码获得数据集的名称。
此外,请注意当您在运行时设置SQL字符串时,设计时的字段列表不能被修改,所以要确保作为您运行时的数据集与设计时的数据集具有相同的字段(或者手动将报表项绑定到字段)。
您在使用产品过程中有任何疑问,可以登录葡萄城开发者社区和经验丰富的技术工程师、ActiveReports开发人员交流:了解更多。
了解ActiveReports产品更多特性:
下载产品体验产品功能: