當Crystal Reports仍然支持ActiveX時,我們會創建水晶報表,以便將ODBC數據源指向Access或SQL數據庫。在運行時,我們會將報告更改爲使用OLE DB(ADO)而不是ODBC,將服務器名稱,數據庫名稱,用戶名,密碼更改爲特定於用戶的密碼,然後運行報告,它工作正常。如何更改代碼中的Crystal報表連接屬性?
既然Crystal Reports 2008不再支持ActiveX,我們正試圖在.NET中做同樣的事情,但沒有成功。
這裏是到目前爲止的代碼:
Public Function ChangeConnectionInfo() As ReportDocument
Dim boReportDocument As New ReportDocument
'**EDIT** Change the path and report name to the report you want to change.
boReportDocument.Load("c:\CustomerListSQL.Rpt", OpenReportMethod.OpenReportByTempCopy)
'Create a new Command Table to replace the reports current table.
Dim boTable As New CrystalDecisions.ReportAppServer.DataDefModel.CommandTable
'boMainPropertyBag: These hold the attributes of the tables ConnectionInfo object
Dim boMainPropertyBag As New PropertyBag
'boInnerPropertyBag: These hold the attributes for the QE_LogonProperties
'In the main property bag (boMainPropertyBag)
Dim boInnerPropertyBag As New PropertyBag
'Set the attributes for the boInnerPropertyBag
boInnerPropertyBag.Add("Auto Translate", "-1")
boInnerPropertyBag.Add("Connect Timeout", "15")
boInnerPropertyBag.Add("Data Source", "K2")
boInnerPropertyBag.Add("General Timeout", "0")
boInnerPropertyBag.Add("Initial Catalog", "DBNAME")
boInnerPropertyBag.Add("Integrated Security", "True")
boInnerPropertyBag.Add("Locale Identifier", "5129")
boInnerPropertyBag.Add("OLE DB Services", "-5")
boInnerPropertyBag.Add("Provider", "SQLOLEDB")
boInnerPropertyBag.Add("Tag with column collation when possible", "0")
boInnerPropertyBag.Add("Use DSN Default Properties", "False")
boInnerPropertyBag.Add("Use Encryption for Data", "0")
'Set the attributes for the boMainPropertyBag
boMainPropertyBag.Add("Database DLL", "crdb_ado.dll")
boMainPropertyBag.Add("QE_DatabaseName", "DBNAME")
boMainPropertyBag.Add("QE_DatabaseType", "OLE DB (ADO)")
'Add the QE_LogonProperties we set in the boInnerPropertyBag Object
boMainPropertyBag.Add("QE_LogonProperties", boInnerPropertyBag)
boMainPropertyBag.Add("QE_ServerDescription", "K2")
boMainPropertyBag.Add("QE_SQLDB", "True")
boMainPropertyBag.Add("SSO Enabled", "False")
'Create a new ConnectionInfo object
Dim boConnectionInfo As New CrystalDecisions.ReportAppServer.DataDefModel.ConnectionInfo
'Pass the database properties to a connection info object
boConnectionInfo.Attributes = boMainPropertyBag
'Set the connection kind
boConnectionInfo.Kind = CrConnectionInfoKindEnum.crConnectionInfoKindCRQE
'**EDIT** Set the User Name and Password if required.
'boConnectionInfo.UserName = "UserName"
'boConnectionInfo.Password = "Password"
'Pass the connection information to the table
boTable.ConnectionInfo = boConnectionInfo
'Get the Database Tables Collection for your report
Dim boTables As CrystalDecisions.ReportAppServer.DataDefModel.Tables = _
boReportDocument.ReportClientDocument.DatabaseController.Database.Tables
'For each table in the report:
' - Set the Table Name properties.
' - Set the Command table's command text.
' - Set the table location in the report to use the new modified table
For Each boReportTable In boTables
boTable.Name = boReportTable.Name
boTable.QualifiedName = "DBNAME.dbo." + boReportTable.Name 'boReportTable.QualifiedName
boTable.Alias = boReportTable.Alias
boReportDocument.ReportClientDocument.DatabaseController.SetTableLocation(boReportTable, boTable)
Next
'Verify the database after adding substituting the new table.
'To ensure that the table updates properly when adding Command tables or Stored Procedures.
boReportDocument.VerifyDatabase()
Return boReportDocument
End Function
代碼工作,直到它到達boReportDocument.VerifyDatabase(),當我得到一個「登錄失敗」錯誤的。
我試圖在這裏使用的代碼,以及:
Dynamically change the connection of a Crystal Report
這也不能工作,我不知道是否有與ODBC切換到OLE DB做(ADO)
不知道如何設置端口? – Mzn 2014-07-21 09:48:56
ServerName屬性應接受「192.168.1.1,1234」,其中1234是端口號。記下IP /服務器名稱後面的冒號。 – KalaNag 2015-05-11 18:10:26