动态更改Crystal Report的连接

suz*_*167 5 c# crystal-reports

我正在使用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.
Run Code Online (Sandbox Code Playgroud)

Dus*_*sty 4

我使用如下函数在运行时分配连接信息。

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);
    }
}
Run Code Online (Sandbox Code Playgroud)

您应该能够简单地创建一个包含必要信息的新 ConnectionInfo 对象,并将其与报告文档一起传递到函数中。希望这可以帮助。