2009-04-20 67 views
4

我有一個水晶報告與50奇怪的子報表,每個負載的參數。將它從一個數據庫切換到另一個數據庫需要很長時間,因爲Crystal Reports IDE堅持要爲每個子報表輸入所有參數。水晶報表配置工具

我想知道是否有可能在C#中編寫一個快速工具來查看rpt文件中所有子報告的當前數據庫配置,並理想地切換到不同的數據庫。

不幸的是(或者幸運的是)我沒有很多Crystal對象模型的經驗 - 任何人都知道從哪裏開始?

謝謝, 喬恩。

+1

你是幸運的;) – dotjoe 2012-07-02 20:22:58

回答

7

這應該做的工作。顯然,在必要時替換密碼和用戶名。

Private Sub ProcessFile(ByVal FileName As String) 
     Dim CR As Engine.ReportDocument = Nothing 
     Try 
      CR = New Engine.ReportDocument 
      CR.Load(FileName, CrystalDecisions.Shared.OpenReportMethod.OpenReportByDefault) 

      'Recurse thru Report 
      RecurseAndRemap(CR) 
      'Save File 
      CR.SaveAs("OutPutFilePath") 

     Catch ex As Exception 
      MessageBox.Show(ex.Message) 
     Finally 
      If Not CR Is Nothing Then 
       CR.Close() 
       CR.Dispose() 
      End If 
     End Try 
    End Sub 

    Private Sub RecurseAndRemap(ByVal CR As Engine.ReportDocument) 
     For Each DSC As CrystalDecisions.Shared.IConnectionInfo In CR.DataSourceConnections 
      DSC.SetLogon("YourUserName", "YourPassword") 
      DSC.SetConnection("YouServerName", "YourDatabaseName", False) 
     Next 

     CR.SetDatabaseLogon("YourUserName", "YourPassword") 

     For Each Table As Engine.Table In CR.Database.Tables 
      Table.LogOnInfo.ConnectionInfo.UserID = "YourUserName" 
      Table.LogOnInfo.ConnectionInfo.Password = "YourPassword" 
     Next 

     If Not CR.IsSubreport Then 
      For Each SR As Engine.ReportDocument In CR.Subreports 
       RecurseAndRemap(SR) 
      Next 
     End If 
    End Sub 

希望幫助乾杯本

1

在VB6中,我們使用這樣的未來(髒複製 - 粘貼形式的舊代碼,增量更新從CR6到CR9),也許你可以得到一些想法:

For Each tmpTable In Report.Database.Tables 
    Set CPProperties = tmpTable.ConnectionProperties 
    CPProperties.DeleteAll 
    CPProperties.Add "Provider", "SQLOLEDB" 
    CPProperties.Add "Data Source", mServerName 
    CPProperties.Add "Initial Catalog", mBaseName 
    CPProperties.Add "User ID", mUserID 
    CPProperties.Add "Password", mPassword 
    CPProperties.Add "Server Name", mServerName 
    CPProperties.Add "Server Type", "OLEDB" 
    CPProperties.Add "DataBase", mBaseName 
    tmpTable.SetTableLocation tmpTable.Location, "", "" 
Next tmpTable 
For Each tmpSection In Report.Sections 
    For Each tmpObject In tmpSection.ReportObjects 
     If TypeName(tmpObject) = "ISubreportObject" Then 
      Set tmpReport = tmpObject.OpenSubreport() 
      For Each tmpTable In tmpReport.Database.Tables 
       Set CPProperties = tmpTable.ConnectionProperties 
       CPProperties.DeleteAll 
       CPProperties.Add "Provider", "SQLOLEDB" 
       CPProperties.Add "Data Source", mServerName 
       CPProperties.Add "Initial Catalog", mBaseName 
       CPProperties.Add "User ID", mUserID 
       CPProperties.Add "Password", mPassword 
       CPProperties.Add "Server Name", mServerName 
       CPProperties.Add "Server Type", "OLEDB" 
       CPProperties.Add "DataBase", mBaseName 
       tmpTable.SetTableLocation tmpTable.Location, "", "" 
      Next tmpTable 
     End If 
    Next tmpObject 
Next tmpSection 
+0

感謝帕特 - 看起來像什麼,我貼在同一時間更完整的版本。 – 2009-04-20 13:30:58

0

實驗位似乎解決了這個問題:

private void Form1_Load(object sender, EventArgs e) 
    { 
     ReportDocument rd = new ReportDocument(); 
     rd.Load("Report.rpt"); 

     Explore(rd); 

     foreach (ReportDocument sr in rd.Subreports) 
     { 
      Explore(sr); 
     } 
    } 

    private void Explore(ReportDocument r) 
    { 
     foreach (IConnectionInfo con in r.DataSourceConnections) 
     { 
      if (!r.IsSubreport) 
       Console.WriteLine("Main Report"); 
      else 
       Console.WriteLine(r.Name); 
      Console.WriteLine(con.DatabaseName); 
      Console.WriteLine("-"); 
     } 
    }