我有一个通过 VS2010 加载的 Crystal Reports 应用程序。我的机器上加载了 CR Runtime 13.0.2。当我使用调试运行应用程序时,它工作正常。(事实上很棒。)但是当我安装构建的应用程序时(即使在非常相同的开发机器上)。TestConnection 行返回错误,这表明它没有正确连接到我的数据库。
如果我不把这段代码放在那里,应用程序会在此行提示输入登录凭据: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