动态更改Crystal Report的连接

时间:2009-09-11 15:54:41

标签: 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.

3 个答案:

答案 0 :(得分:3)

我使用类似下面的函数在运行时分配连接信息。

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对象,并将其与报告文档一起传递给函数。希望这会有所帮助。

答案 1 :(得分:2)

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

答案 2 :(得分:1)

您可以使用以下代码在运行时为报表应用某些连接详细信息。

请在加载报告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;
                    }
                }
            }
        }
    }