Crystal Reports Configuration Tool [closed]

点点圈 提交于 2019-12-22 09:08:58

问题


I have a Crystal report with 50 odd subreports, each with loads of parameters. Switching it from one database to another takes ages as the Crystal Reports IDE insists that you enter all the parameters for each sub-report.

I'm wondering if it's possible to write a quick tool in C# to view the current database config of all of the sub-reports in an rpt file, and ideally to switch to a different database.

Unfortunately (or fortunately) I don't have much experience of the Crystal object model - anyone know where to start?

Thanks, Jon.


回答1:


This should do the job. Obviously replace the passwords and User names where neccesary.

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

Hope that helps Cheers Ben




回答2:


In VB6 we use something like next (dirty copy-paste form old code, incrementally updated from CR6 to CR9), maybe you can get some ideas:

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



回答3:


Bit of experimentation seems to solved the problem:

    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("-");
        }
    }


来源:https://stackoverflow.com/questions/768166/crystal-reports-configuration-tool

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!