问题
I am new to Crystal reports. I have to set the datasource of the report at run time through code. I have a working code but it is very unpredictable and has been giving many problems. I just wanted to know if I am doing something wrong or if there is a better way to do this.
Sometimes I feel the report is not even using the connection string rather using the connection that was specified at compile time of the report from the standalone Crystal application. Is this true?
Dim connectionInfo As New ConnectionInfo
connectionInfo.ServerName = "UID=abc;PWD=abc;Driver= {SQL Server};Server=" & Page.Request.QueryString("server") & ";Database=" & Page.Request.QueryString("database")
Using report As New ReportDocument
report.Load(Server.MapPath("/report/Crystal/test.rpt"))
report.FileName = Server.MapPath("/report/Crystal/test.rpt")
SetDBLogonForReport(connectionInfo, report)
...
End Using
Private Sub SetDBLogonForReport(ByVal connectionInfo As ConnectionInfo, ByVal reportDocument As ReportDocument)
Dim tables As Tables
tables = reportDocument.Database.Tables
For Each table As CrystalDecisions.CrystalReports.Engine.Table In tables
Dim tableLogonInfo As New TableLogOnInfo
tableLogonInfo = table.LogOnInfo
tableLogonInfo.ConnectionInfo = connectionInfo
table.ApplyLogOnInfo(tableLogonInfo)
Next
End Sub
回答1:
This was asked a while ago, but I thought I'd post a response here in case anyone else is looking for the answer. I just used the above code, and it worked fine... with a few exceptions.
I changed:
connectionInfo.ServerName = [Entire ConnectionString?]
to:
connection.DatabaseName = [DatabaseName]
connection.UserID = [UserID]
connection.ServerName = [ServerName]
connection.Password = [Password]
Also, initializing the tableLogonInfo
variable in the loop to New TableLogOnInfo
was unnecessary.
I'm sure, as mentioned in comments above, there are a plethora of examples out there. I just figured I'd add an answer here since it was the first result I landed on when searching for a solution.
来源:https://stackoverflow.com/questions/12517974/proper-way-to-programmatically-set-database-connection-for-crystal-reports