经过更多的研究,我发现有一个两部分的答案。
第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 数据库时,这里有两个关键信息。
- 驱动程序、服务器和端口号都必须在服务器名称属性中指定。
- 如果以数据所有者以外的用户身份连接,则必须为要从中提取数据的每个表指定架构名称。
来源
使用的几个来源没有在我的特定情况下有效的答案,但引导我朝着正确的方向前进。下面列出了这些来源。