问题描述
我是Crystal报表的新手.我必须在运行时通过代码设置报告的数据源.我有一个有效的代码,但是它非常难以预测,并且给了很多问题.我只是想知道我做错了什么还是有更好的方法来做到这一点.
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.
有时候,我觉得该报告甚至没有使用连接字符串,而是使用了在独立Crystal应用程序的报告编译时指定的连接.这是真的吗?
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
推荐答案
这是在不久前问到的,但我认为我会在此处发布回复,以防其他人在寻找答案.我只是使用了上面的代码,并且工作正常……除了一些例外.
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.
我更改了:
connectionInfo.ServerName = [Entire ConnectionString?]
收件人:
connection.DatabaseName = [DatabaseName]
connection.UserID = [UserID]
connection.ServerName = [ServerName]
connection.Password = [Password]
此外,也不需要将循环中的 tableLogonInfo
变量初始化为 New TableLogOnInfo
.
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.
这篇关于以编程方式为Crystal Reports设置数据库连接的正确方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!