Può sembrare un task banale, invece, se si deve cambiare a runtime la stringa di connessione di un report realizzato con Crystal Reports, è necessario scrivere alcune righe di codice. Sul sito di Code Project, all'indirizzo http://www.codeproject.com/useritems/Crystal_Report_Connection.asp, è illustrata una procedura che spiega come fare. Non è niente di eccezionale, ma è comunque uno snippet di codice abbastanza utile.
Al momento questa pagina risulta inaccessibile, ma per fortuna ci viene in aiuto la cara funzione cache di Google. Qui di seguito riporto il codice contenuto nella suddetta pagina:
'' this project creates the crystal report using the ADO.net features
'1. ADD form
'2. Add Crystalviewer on the form
'3. Add dataset from Add new File
'4. Add new element on to the Dataset (the element name should be the same as the name of the field or column
'in the current table )
'5. Add Crytal report from Add new file
'6. Follow the wizard to create connection for the report, Select the project Data and select the Dataset that u create in the project
'7. Select the fields u want to display
' BEST PART IS THAT U CAN CHANGE THE DATABSE CONNECTION AT RUNTIME
' BUT IT SHOULD BE THE SAME DATABSE AND SHOULD HAVE SAME TABLE
' THIS HELPS U WHEN U INSTALL UR APPLICATION ONTO THE USER MACHINE WHERE THE DIRECTORY STRUCTURE WOULD NOT
' BE THE SAME
' TO DO THAT U NEED TO CHANGE THE DATASOURCE PATH NAME
'' THIS WORKS WITH ALL KIND THE DATABASE
'' ALSO MYSQL
'' IF U WANT TO CHANGE THE DATABSE OTHER THAN I USED (MICROSOFT ACCES) U NEED TO CHANGE THE CONNECTION STRING
'' THAT IS THE DRIVER DETAILS AND MAY BE THE FORMAT APPROVED BY THAT DATABSE
'' TO GET THE DIFFRENT DATABSE CONNECTION STRING GOTO http://www.connectionstrings.com/
'' THE CURRENT STRING IS DSNLess STRING AND IT TO THE SAMPLE DATABASE OF CRYSTAL REPORT IN VISUAL STUDIO 2003
'' DATABASE NAME = XTREME.MDB
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim con As New OdbcConnection
Dim com As New OdbcCommand
Dim adp As New OdbcDataAdapter
Dim ds As New Dataset1
ds.Clear()
Dim path = Application.StartupPath.ToString
con.ConnectionString = "Driver={Microsoft Access Driver (*.mdb)};Dbq= C: Files Visual Studio .NET 2003 Reports.mdb;Uid=Admin;Pwd=;"
com.CommandText = "select EmployeeID, [Last Name], FirstName from employee"
com.CommandType = CommandType.Text
com.Connection = con
adp.SelectCommand = com
Try
adp.Fill(ds, "employeeDS")
Catch ex As Odbc.OdbcException
MsgBox(ex.Message)
End Try
Try
Dim myreport As New CrystalReport1
myreport.SetDataSource(ds)
cr.ReportSource = myreport
Catch ex As Exception
MsgBox(ex.Message)
End Try
End Sub
powered by IMHO 1.3