经过更深入的研究,我发现这个问题有两个部分的解决方案。
第一部分
如果您正在通过ODBC连接到PostgreSQL(截至本文撰写时,这是Crystal Reports从PostgreSQL获取数据的唯一方式),并且使用数据所有者,则可以使用以下代码:
reportDoc.Load(report)
reportDoc.DataSourceConnections[0].SetConnection("Driver={PostgreSQL ANSI}
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.
如果您作为拥有报告数据的用户连接,则此方法仅适用,因为不需要提供模式名称。
第二部分
如果您使用数据所有者以外的用户通过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数据库时,有两个关键信息需要注意。
- 必须在服务器名称属性中指定驱动程序、服务器和端口号。
- 如果以数据所有者之外的用户身份连接,则必须为您从中提取数据的每个表指定模式名称。
来源
使用了多个来源,但没有一个答案适用于我的具体情况,但它们引导了我正确的方向。这些来源如下所示。