如何在运行时更改 Crystal Report 的 ODBC 数据库连接?
我有一份用 Crystal Reports 2008 制作的报告,我需要部署一个生产系统,这意味着我需要能够在运行时更改数据库连接。 数据库是 PostgreSQL 8.3.0,我用于创建初始报告的连接是 ODBC 连接。
我找到了各种更改数据库连接的方法,包括以下内容:
reportDoc.Load(report);
reportDoc.DataSourceConnections[0].SetConnection("server", "database", "user", "pwd");
reportDoc.ExportToDisk(CrystalDecisions.Shared.ExportFormatType.PortableDocFormat, path);
但是,这总是失败并显示以下错误消息。
打开连接失败。
我已通过使用 pgAdmin III 成功连接到数据库来验证连接参数,因此我知道连接参数是正确的。 此外,如果我删除 SetConnection(...) 行,则代码如下所示:
reportDoc.Load(report);
reportDoc.ExportToDisk(CrystalDecisions.Shared.ExportFormatType.PortableDocFormat, path);
然后使用存储在报告中的连接参数,报告可以正常运行。 此方法是否可能不适用于 ODBC 连接?
如何在运行时更改 Crystal Report 的 ODBC 数据库连接?
I have a report made with Crystal Reports 2008 that I need to deploy a production system which means that I need to be able to change the database connection at runtime. The database is PostgreSQL 8.3.0 and the connection I use for creating the initial report is an ODBC connection.
I have found various ways to change the database connection including the following:
reportDoc.Load(report);
reportDoc.DataSourceConnections[0].SetConnection("server", "database", "user", "pwd");
reportDoc.ExportToDisk(CrystalDecisions.Shared.ExportFormatType.PortableDocFormat, path);
However, this always fails with the following error message.
Failed to open the connection.
I have validated the connection parameters by successfully connecting to the database with pgAdmin III so I know the connection parameters are correct. In addition, if I remove the SetConnection(...) line so the code looks like this:
reportDoc.Load(report);
reportDoc.ExportToDisk(CrystalDecisions.Shared.ExportFormatType.PortableDocFormat, path);
then the report runs fine using the connection parameters that are stored in the report. Is it possible that this method does not work for ODBC connections?
How do I change a Crystal Report's ODBC database connection at runtime?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
经过更多研究后,我发现答案分为两部分。
第 1 部分
如果您使用数据所有者通过 ODBC(截至撰写本文时 Crystal Reports 从 PostgreSQL 提取数据的唯一方式)连接到 PostgreSQL,则可以使用以下代码:
此方法仅在您连接时才有效作为拥有您正在报告的数据的用户,因为不需要提供架构名称。
第 2 部分
如果您使用数据所有者以外的用户通过 ODBC 连接到 PostgreSQL,则需要手动提供架构名称。 这是通过以下代码完成的。
总结
当尝试从 Crystal Reports 连接到 PostgreSQL 数据库时,有两个关键信息。
资料来源
有几个资料来源没有提供适合我的特定场景的答案,但它们引导我走向正确的方向。 下面列出了这些来源。
After even more research I found that there was a two part answer.
PART 1
If you are connecting to PostgreSQL via ODBC (the only way Crystal Reports can pull data from PostgreSQL as of the time of this writing) using the data owner you then you can use the following code:
This method only works if you are connecting as a user that owns the data that you are reporting on because the schema name does not need to be supplied.
PART 2
If you are connecting to PostgreSQL via ODBC with a user other than the data owner then you need to manually supply the schema name. This is accomplished with the following code.
Summary
There are two critical pieces of information here when trying to connect to a PostgreSQL database from Crystal Reports.
Sources
There were several sources used that did not have an answer that worked in my specific scenario but that led me in the right direction. These sources are listed below.
我刚刚经历过同样的磨难。
我像这样设置连接(其中 sDataSource 等...是包含信息的字符串)
I just went through this same ordeal.
I set my connections like this (where sDataSource etc... are string with the information)
更新 Crystal 报告文件中的 ODBC。
我们正在将 ODBC 与 MSSQL 一起使用,我们找不到如何在 C Sharp 项目中的 Crystal 文件中更新 ODBC。
通过这里提供的示例,我们能够找到在 MSSQL 中更新 ODBC 的方法,就这么简单:
这样,crystal 文件中的 ODBC 就会自动更新。
Updating the ODBC within a crystal report file.
We are using ODBC with MSSQL, we couldn't find how to update the ODBC within the crystal files within a C sharp project.
With the example provided here we were able to find the way to update the ODBC within MSSQL, and it is as simple as this:
With this the ODBC in the crystal file is automaticaly updated.
首先感谢您提供这些信息!!!
我正在使用 MySQL/C#/Crystal Reports。 设置 ODBC/DSN 后,像这样简单的事情对我有用。
First of all thank you for this information!!!
I'm using MySQL/C#/Crystal Reports. After setting up the ODBC/DSN something as simple as this worked for me.
这适用于 Sql Server 2008 R2,用于 DSN less 连接。
This Work for Sql Server 2008 R2 for DSN less connection.