Crystal Report Datasource remap

后端 未结 2 406
情歌与酒
情歌与酒 2020-12-11 13:57

I am working with Crystal Reports and MS SQL Server. I need to remap the crystal report to point to a different database on the same SQL Server. Is there an automated way of

相关标签:
2条回答
  • 2020-12-11 14:25

    You'll find hereafter the procedure I use (I simplified it on the fly, suppressing our own objects and global variables). This procedure allows to redirect a report from an original connection used at development time to the active SQL server. It is written in VB and uses 2 main objects:

    1. The original report object opened through an instance of crystal report
    2. An ADODB connection being the active connection (called P_currentConnection) to the current SQL server

    This function (could be also a sub) is called before viewing/printing the report object in the application. It can be used when distributing reports among replicated databases where users, depending on their location, connect to different servers/databases.

    Public Function connectReportToDatabase( _
        P_report As CRAXDRT.Report)
    
    Dim table As CRAXDRT.DatabaseTable, _
    
    For Each table In P_report.Database.tables
    
        If table.DllName <> "crdb_ado.dll" Then
            table.DllName = "crdb_ado.dll"
        End If
    
        table.ConnectionProperties.DeleteAll
    
        table.ConnectionProperties.Add "Provider", P_currentConnection.Provider
        table.ConnectionProperties.Add "Data source", P_currentConnection.Properties("Data source").Value
        table.ConnectionProperties.Add "Database", P_currentConnection.DefaultDatabase
        table.ConnectionProperties.Add "Integrated security",  P_currentConnection.Properties("Integrated security").Value
        table.ConnectionProperties.Add "Persist Security Info", P_currentConnection.Properties("Persist Security Info").Value
        table.ConnectionProperties.Add "Initial Catalog", P_currentConnection.Properties("Initial Catalog").Value
    
        table.SetTableLocation table.location, "", P_currentConnection.ConnectionString
    
        table.TestConnectivity
    
    Next table
    

    It can be called with a procedure such as:

    Dim crystal As CRAXDRT.Application, _
        m_report as CRAXDRT.report        
    
    Set crystal = New CRAXDRT.Application
    Set m_rapport = crystal.OpenReport(nameOfTheReport & ".rpt")
    
    connectreportToDatabase(m_report)
    

    In case your report includes subreports, You might also have to redirect them to the active connection. In this case, you'll have to browse all objects in your report, check the ones that are of the report type and redirect them to the new connection. I am sure you'll have fun adding the corresponding extra lines to this original procedure.

    0 讨论(0)
  • 2020-12-11 14:46

    You can get any of the info from the current report connection info. So if your not changing servers, then set the crystalServer variable to the reports current server.

    'SET REPORT CONNECTION INFO
    For i = 0 To rsource.ReportDocument.DataSourceConnections.Count - 1
        rsource.ReportDocument.DataSourceConnections(i).SetConnection(crystalServer, crystalDB, crystalUser, crystalPassword)
    Next
    
    For i = 0 To rsource.ReportDocument.Subreports.Count - 1
        For x = 0 To rsource.ReportDocument.Subreports(i).DataSourceConnections.Count - 1
            rsource.ReportDocument.OpenSubreport(rsource.ReportDocument.Subreports(i).Name).DataSourceConnections(x).SetConnection(crystalServer, crystalDB, crystalUser, crystalPassword)
        Next
    Next
    
    0 讨论(0)
提交回复
热议问题