postgresql – 如何在运行时更改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);

但是,始终会出现以下错误消息.

Failed to open the connection.

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

reportDoc.Load(report);
reportDoc.ExportTodisk(CrystalDecisions.Shared.ExportFormatType.PortableDocFormat,path);

那么使用存储在报表中的连接参数就可以很好的报告.这种方法可能不适用于ODBC连接吗?

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

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

第1部分

如果您通过ODBC连接到Postgresql(Crystal Reports可以使用数据所有者从Postgresql获取数据的唯一方法),那么您可以使用以下代码

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.

方法仅在您以拥有所报告数据的用户身份进行连接时才起作用,因为不需要提供模式名称.

第2部分

如果您通过ODBC与除数据所有者之外的用户连接到Postgresql,则需要手动提供模式名称.这是通过以下代码完成的.

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数据库时,这里有两个重要信息.

>必须在服务器名称属性中指定驱动程序,服务器和端口号.
>如果以数据所有者以外的用户进行连接,则必须为要从中拉取数据的每个表指定模式名称.

来源

有几个来源,没有一个答案在我的具体情况下工作,但导致我的方向正确.这些来源列在下面.

> Nathan Koop’s Answer
> https://www.sdn.sap.com/irj/scn/thread?messageID=6913827#6913827
> Best Practices for Changing Databases at Runtime

相关文章

项目需要,有个数据需要导入,拿到手一开始以为是mysql,结果...
本文小编为大家详细介绍“怎么查看PostgreSQL数据库中所有表...
错误现象问题原因这是在远程连接时pg_hba.conf文件没有配置正...
因本地资源有限,在公共测试环境搭建了PGsql环境,从数据库本...
wamp 环境 这个提示就是说你的版本低于10了。 先打印ph...
psycopg2.OperationalError: SSL SYSCALL error: EOF detect...