Crystal Reports配置工具

时间:2009-04-20 13:06:10

标签: crystal-reports

我有一个包含50个奇数子报表的Crystal报表,每个子报表都有大量参数。将它从一个数据库切换到另一个数据库需要很长时间,因为Crystal Reports IDE坚持要为每个子报告输入所有参数。

我想知道是否可以在C#中编写一个快速工具来查看rpt文件中所有子报告的当前数据库配置,理想情况下切换到不同的数据库。

不幸的是(或幸运的是)我对Crystal对象模型没有多少经验 - 任何人都知道从哪里开始?

谢谢, 乔恩。

3 个答案:

答案 0 :(得分: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

希望能帮助Cheers Ben

答案 1 :(得分: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

答案 2 :(得分: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("-");
        }
    }