我有一个Crystal Reports应用程序,我通过VS2010加载。我在我的机器上加载了CR Runtime 13.0.2。当我使用调试运行应用程序时,它工作正常。 (实际上很棒。)但是当我安装构建的应用程序时(即使在非常相同的开发机器上).TestConnection行返回false,这表明它没有正确连接到我的数据库。
如果我没有将此代码放在那里,应用程序会在此行提示输入登录凭据: Me.CrystalReportViewer1.ReportSource = reportDocument1
但是数据库名称和服务器名称不正确并且显示为灰色,并且我放入UserName和PW的任何内容都不起作用。
我搜索了谷歌并尝试了任意数量的“修复程序”,并且没有让它发挥作用。
我还使用数据库专家“更新”我的数据源,并从设计模式运行“验证数据库”,但仍然会发生同样的事情。
这是我的代码:
Private Function ConnectReport(sDatabaseFile As String, _serverName As String, ReportDocument1 As ReportDocument)
ReportDocument1.SetDatabaseLogon("sa", "sqlAdmin2008", _serverName, sDatabaseFile, True)
For x As Integer = 0 To ReportDocument1.DataSourceConnections.Count - 1
ReportDocument1.DataSourceConnections(x).SetConnection(_serverName, sDatabaseFile, "sa", "sqlAdmin2008")
Next
For Each cTable As Table In ReportDocument1.Database.Tables
If cTable.Name <> "Command" Then
SetTableConnectionInfo(cTable, sDatabaseFile, _serverName)
End If
Next
For Each obj As ReportObject In ReportDocument1.ReportDefinition.ReportObjects
If obj.Kind = ReportObjectKind.SubreportObject Then
Dim subReport As SubreportObject = CType(obj, SubreportObject)
Dim subReportDocument As ReportDocument = ReportDocument1.OpenSubreport(subReport.SubreportName)
ConnectReport(sDatabaseFile, _serverName, subReportDocument)
End If
Next
End Function
Private Function SetTableConnectionInfo(cTable As Table, sDatabaseFile As String, _serverName As String)
Dim logonInfo As TableLogOnInfo = cTable.LogOnInfo
Dim connInfo As ConnectionInfo = New ConnectionInfo()
connInfo.DatabaseName = sDatabaseFile
connInfo.ServerName = _serverName
connInfo.UserID = "sa"
connInfo.Password = "sqlAdmin2008"
'connInfo.Type = ConnectionInfoType.SQL
logonInfo.ConnectionInfo = connInfo
cTable.ApplyLogOnInfo(logonInfo)
If cTable.TestConnectivity = False Then
Throw New ApplicationException("Cannot connect Crystal Reports to Database.")
End If
cTable.Location = sDatabaseFile & "." & "dbo" & "." & cTable.Location
End Function
答案 0 :(得分:0)
在我看来,你需要将表格作为参考传递给你。
Private Function SetTableConnectionInfo(ref cTable As Table, sDatabaseFile As String, _serverName As String)