Vb.net – How to change Crystal Reports connection string using OLE DB in vb.net

connectioncrystal-reportsoledbvb.net

i have a "Connection.vb" where all my Connection string stored and used by the entire program.

i'm new to Crystal Reports.. and i want to changes its connection string based on the connection string i made… im using MS SQL 2008.. SQL Server Native Client 10.0 OLE DB Provider..

and here is my connection string.
"Provider=SQLNCLI10;Server=….\SQLEXPRESS;Database=Blah;Trusted_Connection=yes"

i tried to google it.. but they are using different language, different database, and complex examples…

thanks in advance..

Best Answer

I just spent some quality time figuring out this exact problem, in VB. I hope this helps.

Replace YourDataContext() with whatever your data context is. Alternatively, you can get the connection string from the AppConfig as usual. I am getting mine from the data context because we are migrating databases and keep changing the connection strings.

   Private Sub SetCrystalReportsConnection(ByRef report As ReportDocument)

    Dim sqlConnInfo As SqlConnectionStringBuilder = New SqlConnectionStringBuilder(New YourDataContext().Connection.ConnectionString)

    For Each connection As InternalConnectionInfo In report.DataSourceConnections
        If sqlConnInfo.IntegratedSecurity Then
            connection.SetConnection(sqlConnInfo.DataSource, sqlConnInfo.InitialCatalog, True)
        Else
            connection.SetConnection(sqlConnInfo.DataSource, sqlConnInfo.InitialCatalog, sqlConnInfo.UserID, sqlConnInfo.Password)
            connection.IntegratedSecurity = False
        End If
    Next

End Sub
Related Topic