我有一份使用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数据库连接?
[甚至更多研究之后,我发现答案分为两部分。
如果您使用数据所有者通过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.
此方法仅在您以拥有要报告的数据的用户身份进行连接时才有效,因为不需要提供架构名称。
如果您以除数据所有者以外的其他用户通过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数据库时,这里有两个重要的信息。
[使用过的几种资料来源在我的特定情况下没有答案,但却使我朝着正确的方向前进。这些来源在下面列出。
我刚刚经历了同样的考验。
我这样设置我的连接(其中sDataSource等...是带有信息的字符串)
Dim myConnectionInfo As ConnectionInfo = New ConnectionInfo()
myConnectionInfo.ServerName = sDataSource
myConnectionInfo.DatabaseName = sInitialCatalog
myConnectionInfo.UserID = sUserID
myConnectionInfo.Password = sPassword
Dim myTables As Tables = report.Database.Tables
Dim myTableLogonInfo As TableLogOnInfo = New TableLogOnInfo()
myTableLogonInfo.ConnectionInfo = myConnectionInfo
For Each myTable As CrystalDecisions.CrystalReports.Engine.Table In myTables
myTable.ApplyLogOnInfo(myTableLogonInfo)
myTable.LogOnInfo.ConnectionInfo.DatabaseName = myTableLogonInfo.ConnectionInfo.DatabaseName
myTable.LogOnInfo.ConnectionInfo.ServerName = myTableLogonInfo.ConnectionInfo.ServerName
myTable.LogOnInfo.ConnectionInfo.UserID = myTableLogonInfo.ConnectionInfo.UserID
myTable.LogOnInfo.ConnectionInfo.Password = myTableLogonInfo.ConnectionInfo.Password
Next
更新水晶报表文件中的ODBC。
我们将ODBC与MSSQL一起使用,我们找不到在C Sharp项目中的Crystal文件中如何更新ODBC的方法。
通过此处提供的示例,我们能够找到在MSSQL中更新ODBC的方法,它很简单:
Cursor.Current = Cursors.WaitCursor;
CrystalDecisions.Windows.Forms.CrystalReportViewer CR_Viewer;
CR_Viewer = new CrystalDecisions.Windows.Forms.CrystalReportViewer();
this.Controls.Add(CR_Viewer);
ConnectionInfo connInfo = new ConnectionInfo();
connInfo.ServerName = "YOUR ODBC NAME";
TableLogOnInfo tableLogOnInfo = new TableLogOnInfo();
tableLogOnInfo.ConnectionInfo = connInfo;
//THIS IS A CRYSTAL RPT FILE DIFINE AS A CLASS
Facturation_Nord_Ouest.Reports.Facture_Français CrystalReportFr;
CrystalReportFr = new Facturation_Nord_Ouest.Reports.Facture_Français();
for (int i = 0; i < CrystalReportFr.Database.Tables.Count; i++)
{
CrystalReportFr.Database.Tables[i].ApplyLogOnInfo(tableLogOnInfo);
}
CR_Viewer.ReportSource = CrystalReportFr;
CR_Viewer.ActiveViewIndex = 0;
CR_Viewer.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
CR_Viewer.Dock = System.Windows.Forms.DockStyle.Fill;
CR_Viewer.Location = new System.Drawing.Point(0, 0);
CR_Viewer.Size = new System.Drawing.Size(545, 379);
CR_Viewer.TabIndex = 0;
CR_Viewer.Name = "Invoice";
CR_Viewer.Zoom(100);
CR_Viewer.Show();
Cursor.Current = Cursors.Default;
使用此方法,自动更新水晶文件中的ODBC。
首先感谢您提供此信息!!
我正在使用MySQL / C#/ Crystal Reports。设置ODBC / DSN之后,对我来说很简单。
using CrystalDecisions.CrystalReports.Engine;
using CrystalDecisions.Shared;
using MySql.Data.MySqlClient;
.
.
.
ConnectionInfo connInfo = new ConnectionInfo();
connInfo.ServerName = "Driver={MySQL ODBC 3.51 Driver};DSN=MyODBCDatasourceName";
TableLogOnInfo tableLogOnInfo = new TableLogOnInfo();
tableLogOnInfo.ConnectionInfo = connInfo;
// rpt is my Crystal Reports ReportDocument
// Apply the schema name to the table's location
foreach (Table table in rpt.Database.Tables)
{
table.ApplyLogOnInfo(tableLogOnInfo);
table.Location = table.Location;
}
protected void Page_Load(object sender, EventArgs e)
{
try
{
ReportDocument cryRpt = new ReportDocument();
TableLogOnInfos crtableLogoninfos = new TableLogOnInfos();
TableLogOnInfo crtableLogoninfo = new TableLogOnInfo();
ConnectionInfo crConnectionInfo = new ConnectionInfo();
cryRpt.Load(@"D:\tem\WebAppReport\WebAppReport\CrystalReport1.rpt");
crConnectionInfo.ServerName = "misserver";
crConnectionInfo.DatabaseName = "testAccountability_data";
crConnectionInfo.UserID = "RW";
crConnectionInfo.Password = "RW";
foreach (CrystalDecisions.CrystalReports.Engine.Table CrTable in cryRpt.Database.Tables)
{
crtableLogoninfo = CrTable.LogOnInfo;
crtableLogoninfo.ConnectionInfo = crConnectionInfo;
CrTable.ApplyLogOnInfo(crtableLogoninfo);
}
CrystalReportViewer1.ReportSource = cryRpt;
CrystalReportViewer1.RefreshReport();
}
catch
{
}
}
此用于Sql Server 2008 R2的DSN较少连接的方法。
Dim myConnectionInfo As CrystalDecisions.Shared.ConnectionInfo = New CrystalDecisions.Shared.ConnectionInfo()
myConnectionInfo.ServerName = "Driver={SQL Server Native Client 10.0};Server=P03\sqlrs1;"
myConnectionInfo.DatabaseName = "RS1DB"
myConnectionInfo.UserID = "user"
myConnectionInfo.Password = "pwd"
For Each myTable As CrystalDecisions.CrystalReports.Engine.Table In mboReportDocument.Database.Tables
Dim myTableLogonInfo As TableLogOnInfo = myTable.LogOnInfo
myTableLogonInfo.ConnectionInfo = myConnectionInfo
myTable.ApplyLogOnInfo(myTableLogonInfo)
Next
我尝试使用上面的代码,但是出现登录失败错误
cryRpt.Load(cryRptPath)
Dim connInfo = New ConnectionInfo()
connInfo.ServerName = "Driver={PostgreSQL ANSI};Server=192.168.243.1;Port=5432;"
connInfo.DatabaseName = "postgres"
connInfo.UserID = "postgres"
connInfo.Password = "ragu1234"
Dim TableLogOnInfo = New TableLogOnInfo()
TableLogOnInfo.ConnectionInfo = connInfo
For Each Table In cryRpt.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 = "public." + Table.Location
Next
cryRpt.Refresh()
cryRpt.SetDatabaseLogon(strDbName, strDbPassWord, strDbServer, strDbName)
cryRpt.VerifyDatabase()