Crystal Report Code Setting Connection Info

2011 September 27

Microsoft VB.NET

        Try
            Dim cryRpt As New ReportDocument
            cryRpt.Load(My.Settings.DirReport & "\" & Form1.cmbReportSample.Text)

            Dim crConnectionInfo As New ConnectionInfo
            With crConnectionInfo
                'physical server name 
                .ServerName = My.Settings.OracleServizio
                ' Note: you do not need to set .DatabaseName for Oracle.
                .DatabaseName = ""
                .UserID = My.Settings.OracleUtente
                .Password = My.Settings.OraclePassword
            End With

            'Get the table information from the report
            Dim crDatabase As Database
            crDatabase = cryRpt.Database

            Dim crTables As Tables
            crTables = crDatabase.Tables
            Dim crtable As Table

            'Loop through all tables in the report and apply the connection()
            'information for each table.
            Dim crTableLogOnInfo As TableLogOnInfo

            For Each crtable In crTables
                crTableLogOnInfo = crtable.LogOnInfo
                crTableLogOnInfo.ConnectionInfo = crConnectionInfo
                crtable.ApplyLogOnInfo(crTableLogOnInfo)
                crtable.Location = My.Settings.OracleUtente & "." & crtable.Location
                'MessageBox.Show(crtable.Location)
            Next

            'Aggiornamento Sottoreports
            Dim crSections As Sections
            crSections = cryRpt.ReportDefinition.Sections
            Dim crSection As Section
            Dim crReportObjects As ReportObjects
            Dim crReportObject As ReportObject
            Dim crSubreportObject As SubreportObject
            Dim subRepDoc As ReportDocument
            For Each crSection In crSections
                crReportObjects = crSection.ReportObjects
                For Each crReportObject In crReportObjects
                    If crReportObject.Kind = ReportObjectKind.SubreportObject Then
                        MessageBox.Show("Sottoreport")
                        crSubreportObject = crReportObject
                        subRepDoc = crSubreportObject.OpenSubreport(crSubreportObject.SubreportName)
                        crDatabase = subRepDoc.Database
                        crTables = crDatabase.Tables

                        For Each crtable In crTables
                            crTableLogOnInfo = crtable.LogOnInfo
                            crTableLogOnInfo.ConnectionInfo = crConnectionInfo
                            crtable.ApplyLogOnInfo(crTableLogOnInfo)
                            crtable.Location = My.Settings.OracleUtente & "." & crtable.Location
                            'MessageBox.Show(crtable.Location)
                        Next

                    End If

                Next

            Next

            cryRpt.Refresh()
            CrystalReportViewer.ReportSource = cryRpt

        Catch exc As Exception
            MessageBox.Show("Errore, impossibile aprire il report!" & vbCrLf & exc.ToString, "Errore", MessageBoxButtons.OK, MessageBoxIcon.Error)
        End Try

Microsoft C#

private void print_report() {
	Dim rpt1 As New CrystalDecisions.CrystalReports.Engine.ReportDocument
	rpt1.Load(Application.StartupPath & "\rpt\ReportTest1.rpt")
	SetDbLogon(rpt1, "127.0.0.1", "myuser1", "mypassword1", "mynamedatabase")

	rpt1.PrintOptions.PrinterName = "myprinter"	
	rpt1.PrintToPrinter(1, True, 0, 0)

	rpt1 = Nothing
}

Private Sub SetDbLogon(ByRef rptDoc As CrystalDecisions.CrystalReports.Engine.ReportDocument, 
                        ByVal strServerName As String, ByVal strUserId As String, ByVal strPassword As String, 
                        ByVal strDatabaseName As String)
        Dim rptTable As CrystalDecisions.CrystalReports.Engine.Table
        Dim rptSub As CrystalDecisions.CrystalReports.Engine.ReportDocument
        Dim rptSubDoc As CrystalDecisions.CrystalReports.Engine.ReportDocument
        Dim myLogonInfo As CrystalDecisions.Shared.TableLogOnInfo
        Try
            For Each rptTable In rptDoc.Database.Tables 
                myLogonInfo = rptTable.LogOnInfo
                With myLogonInfo.ConnectionInfo
                    .ServerName = strServerName
                    .DatabaseName = strDatabaseName
                    .UserID = strUserId
                    .Password = strPassword

                End With
                rptTable.ApplyLogOnInfo(myLogonInfo)

                rptTable.Location = strDatabaseName + ".dbo." & rptTable.Location.Substring(rptTable.Location.LastIndexOf(".") + 1)
            Next
            For Each rptSub In rptDoc.Subreports
                rptSubDoc = rptDoc.OpenSubreport(rptSub.Name)
                For Each rptTable In rptSubDoc.Database.Tables 
                    myLogonInfo = rptTable.LogOnInfo
                    With myLogonInfo.ConnectionInfo
                        .ServerName = strServerName
                        .DatabaseName = strDatabaseName
                        .UserID = strUserId
                        .Password = strPassword
                    End With
                    rptTable.ApplyLogOnInfo(myLogonInfo)
                    rptTable.Location = strDatabaseName + ".dbo." & rptTable.Location.Substring(rptTable.Location.LastIndexOf(".") + 1)
                Next
            Next
        Catch ex As Exception
            Throw
        Finally
            If Not rptTable Is Nothing Then
                rptTable.Dispose()
            End If
        End Try

End Sub