2009-09-11 80 views
5

我正在使用CrystalReportViewer和CrystalReportSource在我的應用程序中加載和顯示.rpt文件。動態更改Crystal Report的連接

我的情況是這樣的:

說一個人創造了一個水晶報表我的應用程序之外和數據源設置爲數據庫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

我用一個函數類似下面的連接信息,在運行時分配。

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對象提供必要的信息,並把它傳遞到功能與報表文檔一起。希望這可以幫助。

+0

嗨Dusty ...感謝您的幫助。我實現了上面的代碼行,我也自己做了一些研究。所以hereis我的代碼: 私人無效AssignConnectionInfo(的ReportDocument文件,ConnectionInfo crConnection) { 的foreach(CrystalDecisions.CrystalReports.Engine.Table crTable在document.Database.Tables) { – suzi167 2009-09-14 19:48:47

+1

這裏是代碼 - 遺憾擊中門柱太早之前: 私人無效AssignConnection(的ReportDocument文件,ConnectionInfo crConnection) { 的foreach(CrystalDecisions.CrystalReports.Engine.Table crTable在document.Database.Tables) { CrystalDecisions.Shared.TableLogOnInfo tableLogonInfo = crTable.LogOnInfo; tableLogonInfo.ConnectionInfo = crConnection; crTable.ApplyLogOnInfo(tableLogonInfo); CrystalReportViewer1.ReportSource = document; CrystalReportViewer1.RefreshReport(); } crConnection在經過時具有正確的值。 – suzi167 2009-09-14 19:52:54

+0

格式化不是很好的方式...有沒有一種方法來格式化我的消息(如標籤或其他東西) – suzi167 2009-09-14 19:53:57

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 
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; 
        } 
       } 
      } 
     } 
    }