如何在运行时更改 Crystal Report 的 ODBC 数据库连接? [英] How do I change a Crystal Report's ODBC database connection at runtime?

查看:18
本文介绍了如何在运行时更改 Crystal Report 的 ODBC 数据库连接?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个使用 Crystal Reports 2008 制作的报告,我需要部署一个生产系统,这意味着我需要能够在运行时更改数据库连接.数据库是 PostgreSQL 8.3.0,我用于创建初始报告的连接是 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.

打开连接失败.

我已经通过使用 pgAdmin III 成功连接到数据库来验证连接参数,所以我知道连接参数是正确的.此外,如果我删除 SetConnection(...) 行,代码如下所示:

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);

然后使用存储在报告中的连接参数,报告运行良好.这种方法是否可能不适用于 ODBC 连接?

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?

如何在运行时更改 Crystal Report 的 ODBC 数据库连接?

How do I change a Crystal Report's ODBC database connection at runtime?

推荐答案

经过更多研究后,我发现答案分为两部分.

After even more research I found that there was a two part answer.

如果您使用数据所有者通过 ODBC 连接到 PostgreSQL(截至撰写本文时 Crystal Reports 可以从 PostgreSQL 中提取数据的唯一方式),那么您可以使用以下代码:

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:

reportDoc.Load(report);
reportDoc.DataSourceConnections[0].SetConnection("Driver={PostgreSQL ANSI};Server=myServer;Port=5432;", "myDatabase", "myUser", "myPassword");
reportDoc.ExportToDisk(CrystalDecisions.Shared.ExportFormatType.PortableDocFormat, path);
// Depending on your application you may have more than one data source connection that needs to be changed.

此方法仅在您以拥有您所报告的数据的用户身份连接时才有效,因为不需要提供架构名称.

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.

如果您使用数据所有者以外的用户通过 ODBC 连接到 PostgreSQL,那么您需要手动提供架构名称.这是通过以下代码完成的.

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.

reportDoc.Load(report);

ConnectionInfo connInfo = new ConnectionInfo();
connInfo.ServerName = "Driver={PostgreSQL ANSI};Server=myServer;Port=5432;";
connInfo.DatabaseName = "myDatabase";
connInfo.UserID = "myUser";
connInfo.Password = "myPassword";

TableLogOnInfo tableLogOnInfo = new TableLogOnInfo();
tableLogOnInfo.ConnectionInfo = connInfo;

foreach (Table table in reportDoc.Database.Tables)
{
    table.ApplyLogOnInfo(tableLogOnInfo);
    table.LogOnInfo.ConnectionInfo.ServerName = connInfo.ServerName;
    table.LogOnInfo.ConnectionInfo.DatabaseName = connInfo.DatabaseName;
    table.LogOnInfo.ConnectionInfo.UserID = connInfo.UserID;
    table.LogOnInfo.ConnectionInfo.Password = connInfo.Password;

    // Apply the schema name to the table's location
    table.Location = "mySchema." + table.Location;
}

reportDoc.ExportToDisk(CrystalDecisions.Shared.ExportFormatType.PortableDocFormat, path);

总结

在尝试从 Crystal Reports 连接到 PostgreSQL 数据库时,这里有两个关键信息.

Summary

There are two critical pieces of information here when trying to connect to a PostgreSQL database from Crystal Reports.

  1. 驱动程序、服务器和端口号都必须在服务器名称属性中指定.
  2. 如果以数据所有者以外的用户身份连接,则必须为要从中提取数据的每个表指定架构名称.

来源

使用的多个来源没有在我的特定场景中有效的答案,但引导我朝着正确的方向前进.下面列出了这些来源.

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.

这篇关于如何在运行时更改 Crystal Report 的 ODBC 数据库连接?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆