【发布时间】:2012-04-19 10:06:02
【问题描述】:
我已经为此苦苦挣扎了 4 天。 我有一个非常简单的水晶报告(我只是将它用于概念证明)。该报表绑定到一个数据库,我只显示数据库中一个表中的一个字段。没有子报表。它是使用 Crystal Reports 2008 创建的。我需要在我的 .Net MVC Web 应用程序中显示此报表,但我需要能够更改自此应用程序以来的数据库连接信息。将用于具有相同表结构的不同数据库。 因此,我创建了一个标准的 Web 表单,并将 CrystalReportViewer 和 CrystalReportSource 拖到其中。
这是我的代码:
protected void Page_Load(object sender, EventArgs e)
{
this.CrystalReportSource1.EnableCaching = false;
this.CrystalReportSource1.ReportDocument.Load(@"C:\ReportName.rpt");
//1) I get the data connection variables from my app - this part works well
and is irrelevant in this case.
//2) Once I have the data I need to apply it to the connection of the report
ConnectionInfo crConnection = new ConnectionInfo();
crConnection.UserID = userID;
crConnection.ServerName = datasource;
crConnection.DatabaseName = "";
crConnection.Password = password;
AssignConnectionInfo(CrystalReportSource1.ReportDocument,crConnection);
CrystalReportSource1.ReportDocument.DataSourceConnections[0].SetConnection
(crConnection.ServerName, crConnection.DatabaseName, false);
CrystalReportSource1.ReportDocument.SetDatabaseLogon(crConnection.UserID,
crConnection.Password, crConnection.ServerName, crConnection.DatabaseName);
CrystalReportViewer1.ReportSource = CrystalReportSource1.ReportDocument;
CrystalReportViewer1.RefreshReport();
}//close the page load function
这是 AssignConnectionInfo 函数:
private void AssignConnectionInfo(ReportDocument document,ConnectionInfo crConnection)
{
foreach (CrystalDecisions.CrystalReports.Engine.Table table in document.Database.Tables)
{
TableLogOnInfo logOnInfo = table.LogOnInfo;
if (logOnInfo != null)
{
table.ApplyLogOnInfo(table.LogOnInfo);
table.LogOnInfo.TableName = table.Name;
table.LogOnInfo.ConnectionInfo.UserID = crConnection.UserID;
table.LogOnInfo.ConnectionInfo.Password = crConnection.Password;
table.LogOnInfo.ConnectionInfo.DatabaseName = crConnection.DatabaseName;
table.LogOnInfo.ConnectionInfo.ServerName = crConnection.ServerName;
CrystalReportViewer1.LogOnInfo.Add(table.LogOnInfo);
}
}
}
所以发生的情况是页面加载和 Crystal 横幅、工具栏显示,但我的报告中的数据绑定字段为空白。 有什么不对的地方吗?
非常感谢提前
苏珊
【问题讨论】:
标签: crystal-reports