加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

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

发布时间:2020-12-13 16:31:56 所属栏目:百科 来源:网络整理
导读:我有一份Crystal Reports 2008报告,我需要部署一个生产系统,这意味着我需要能够在运行时更改数据库连接.数据库是PostgreSQL 8.3.0,我用于创建初始报告的连接是ODBC连接. 我已经找到了改变数据库连接的各种方法,包括: reportDoc.Load(report);reportDoc.Data
我有一份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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读