动态更改Crystal Report的连接
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了动态更改Crystal Report的连接相关的知识,希望对你有一定的参考价值。
我正在使用CrystalReportViewer和CrystalReportSource在我的应用程序中加载和显示.rpt文件。
我的情况是这样的:
假设一个人在我的应用程序之外创建了一个水晶报告并将其数据源设置为数据库A.然后我在我的应用程序中使用该.rpt文件,但我需要将它绑定到另一个数据库(就表结构而言与原始数据库相同)和列名,但使用不同的用户名和密码使用不同的连接字符串)。我如何在C#中做到这一点?
目前我使用以下方式加载报告:
this.CrystalReportSource1.ReportDocument.Load(reportsSubfolder + report.ReportFileName);
//it is here that I need to change the connection data of the report.
答案
我使用类似下面的函数在运行时分配连接信息。
private void SetDBLogonForReport(CrystalDecisions.Shared.ConnectionInfo connectionInfo, CrystalDecisions.CrystalReports.Engine.ReportDocument reportDocument)
{
CrystalDecisions.CrystalReports.Engine.Tables tables = reportDocument.Database.Tables;
foreach (CrystalDecisions.CrystalReports.Engine.Table table in tables)
{
CrystalDecisions.Shared.TableLogOnInfo tableLogonInfo = table.LogOnInfo;
tableLogonInfo.ConnectionInfo = connectionInfo;
table.ApplyLogOnInfo(tableLogonInfo);
}
}
您应该能够使用必要的信息简单地创建一个新的ConnectionInfo对象,并将其与报告文档一起传递给函数。希望这可以帮助。
另一答案
VB代码:
Dim report = New ReportDocument
Try
'open report
report.Load(filename, OpenReportMethod.OpenReportByTempCopy)
'do this for each distinct database connection, rather than for table
report.SetDatabaseLogon("user", "password", "server", "database")
Catch ex As Exception
'preserve the stack trace information
Throw
End Try
另一答案
您可以使用以下代码在运行时为报表应用某些连接详细信息。
请在加载报告rpt文件后立即使用该方法,并将所需的连接详细信息传递给方法,它将从传递的连接详细信息中获取报告数据。
public static void CrystalReportLogOn(ReportDocument reportParameters,
string serverName,
string databaseName,
string userName,
string password)
{
TableLogOnInfo logOnInfo;
ReportDocument subRd;
Sections sects;
ReportObjects ros;
SubreportObject sro;
if (reportParameters == null)
{
throw new ArgumentNullException("reportParameters");
}
try
{
foreach (CrystalDecisions.CrystalReports.Engine.Table t in reportParameters.Database.Tables)
{
logOnInfo = t.LogOnInfo;
logOnInfo.ReportName = reportParameters.Name;
logOnInfo.ConnectionInfo.ServerName = serverName;
logOnInfo.ConnectionInfo.DatabaseName = databaseName;
logOnInfo.ConnectionInfo.UserID = userName;
logOnInfo.ConnectionInfo.Password = password;
logOnInfo.TableName = t.Name;
t.ApplyLogOnInfo(logOnInfo);
t.Location = t.Name;
}
}
catch
{
throw;
}
sects = reportParameters.ReportDefinition.Sections;
foreach (Section sect in sects)
{
ros = sect.ReportObjects;
foreach (ReportObject ro in ros)
{
if (ro.Kind == ReportObjectKind.SubreportObject)
{
sro = (SubreportObject)ro;
subRd = sro.OpenSubreport(sro.SubreportName);
try
{
foreach (CrystalDecisions.CrystalReports.Engine.Table t in subRd.Database.Tables)
{
logOnInfo = t.LogOnInfo;
logOnInfo.ReportName = reportParameters.Name;
logOnInfo.ConnectionInfo.ServerName = serverName;
logOnInfo.ConnectionInfo.DatabaseName = databaseName;
logOnInfo.ConnectionInfo.UserID = userName;
logOnInfo.ConnectionInfo.Password = password;
logOnInfo.TableName = t.Name;
t.ApplyLogOnInfo(logOnInfo);
}
}
catch
{
throw;
}
}
}
}
}
以上是关于动态更改Crystal Report的连接的主要内容,如果未能解决你的问题,请参考以下文章
在 C# 中运行时更改 Crystal Report 数据源(访问)
Crystal Report通过变量传递图像URL时未显示动态图像