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); 但是,始终会出现以下错误消息.
我已经通过使用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 (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |