【问题标题】:Dynamically change the connection of a Crystal Report动态更改水晶报表的连接
【发布时间】:2009-09-11 15:54:41
【问题描述】:

我正在使用 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.

【问题讨论】:

    标签: c# crystal-reports


    【解决方案1】:

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

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

    【讨论】:

    • 嗨 Dusty ...感谢您的帮助。我实现了上面的代码行,我自己也做了一些研究。所以这是我的代码: private void AssignConnectionInfo(ReportDocument document, ConnectionInfo crConnection) { foreach (CrystalDecisions.CrystalReports.Engine.Table crTable in document.Database.Tables) {
    • 这里是代码 - 抱歉之前打的太早了:private void AssignConnection(ReportDocument document,ConnectionInfo crConnection) { foreach (CrystalDecisions.CrystalReports.Engine.Table crTable in document.Database.Tables) { CrystalDecisions.Shared.TableLogOnInfo tableLogonInfo = crTable.LogOnInfo; tableLogonInfo.ConnectionInfo = crConnection; crTable.ApplyLogOnInfo(tableLogonInfo); CrystalReportViewer1.ReportSource = 文档; CrystalReportViewer1.RefreshReport(); } crConnection 在通过时具有正确的值。
    • 顺便说一下格式不是很好...有没有办法格式化我的消息(比如标签什么的)
    • @suzi167 - 您可以使用此代码编辑您的问题,这样您就可以按照自己的意愿对其进行格式化。看起来 AssignConnection 函数与我的答案中的函数基本相同,所以我认为这会起作用。出现任何错误时,您是否会收到错误消息?
    【解决方案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
    

    【讨论】:

      【解决方案3】:

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

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-04-22
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多