如何在代码中更改水晶报表连接属性?
当 Crystal Reports 仍然支持 ActiveX 时,我们的 Crystal Reports 是为使用 ODBC 数据源指向 Access 或 SQL 数据库而构建的。在运行时,我们将报告更改为使用 OLE DB (ADO) 而不是 ODBC,将服务器名称、数据库名称、用户名、密码更改为特定于用户的名称,然后运行报告,效果很好。
现在 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(),此时我收到“登录失败”错误。
我也尝试使用此处的代码:
也不起作用,我不知道这是否与从 ODBC 切换到 OLE DB (ADO) 有关
Back when Crystal Reports still supports ActiveX, we have crystal reports that were built to use ODBC data source point to an Access or SQL database. In run time, we would change the report to use OLE DB (ADO) instead of ODBC, change the server name, database name, username, password to ones specific to the user, and run the report, it worked fine.
Now that Crystal Reports 2008 no longer supports ActiveX, we're trying to do the same thing in .NET, but without success.
Here's the code so far:
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
The code works until it gets to boReportDocument.VerifyDatabase(), that's when I get a 'Log On Failed' error.
I tried using the code in here as well:
Dynamically change the connection of a Crystal Report
That didn't work either, I don't know whether it has to do with switching from ODBC to OLE DB (ADO)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我意识到这个问题已经有 3 年历史了,OP 可能已经继续前进了,但这也许可以帮助其他人寻找答案。
这是从 VB.NET 加载报告并更改连接信息和参数值等其他内容的完整示例,从日常用于从多个数据源进行报告的工作应用程序中获取和简化:
I realize this question is 3 years old and the OP probably already moved on, but maybe this can help someone else looking for answers.
This is a full example of loading a Report from VB.NET changing the connection info and other stuff like Parameter values, taken and simplified from a working application used daily for reporting from multiple datasources:
您可以简单地使用此方法登录任何您想要的水晶报表。
只需传递您的参数:
并像这样调用它:
You can simply use this method to Log On any Crystal Report you want.
Just pass your parameters :
And Call it like this :