2
我有通過VS2010加載的Crystal Reports應用程序。我的機器上裝有CR Runtime 13.0.2。當我使用調試運行應用程序時,它工作正常。 (事實上很好。)但是,當我安裝構建的應用程序時(甚至在非常相同的開發機器上),TestConnection行會返回false,這表明它沒有正確連接到我的數據庫。Crystal Reports TestConnection失敗
如果我不把這個代碼在那裏,應用程序在該行提示登錄憑據: Me.CrystalReportViewer1.ReportSource = reportDocument1
但DB名和服務器名是不正確的和灰色的,並且我在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