动态更改Crystal Report的连接 [英] Dynamically change the connection of a Crystal Report
问题描述
我正在使用CrystalReportViewer和CrystalReportSource在应用程序中加载并显示.rpt文件。
I am using CrystalReportViewer and CrystalReportSource to load and display an .rpt file in my application.
我遇到的情况是:
说一个人在我的应用程序外部创建了一个水晶报表,并将其数据源设置为数据库A。然后我在应用程序中使用该.rpt文件,但我需要将其绑定到其他数据库(与就表结构和列名而言,是原始版本,但使用不同的用户名和密码使用不同的连接字符串)。
Say a person created a crystal report outside of my application and set its datasource to database A. I then use that .rpt file in my application but I need to bind it to a different database (identical to the original one in terms of table structure and column names but with a different connection string using a different user name and password). How do I do that in C#?
当前我使用以下方式加载报告:
Currently I load the report using:
this.CrystalReportSource1.ReportDocument.Load(reportsSubfolder + report.ReportFileName);
//it is here that I need to change the connection data of the report.
推荐答案
我使用类似以下的函数来分配连接
I use a function like the following to assign the connection information at runtime.
private void SetDBLogonForReport(CrystalDecisions.Shared.ConnectionInfo connectionInfo, CrystalDecisions.CrystalReports.Engine.ReportDocument reportDocument)
{
CrystalDecisions.CrystalReports.Engine.Tables tables = reportDocument.Database.Tables;
foreach (CrystalDecisions.CrystalReports.Engine.Table table in tables)
{
CrystalDecisions.Shared.TableLogOnInfo tableLogonInfo = table.LogOnInfo;
tableLogonInfo.ConnectionInfo = connectionInfo;
table.ApplyLogOnInfo(tableLogonInfo);
}
}
您应该能够简单地创建一个新的ConnectionInfo对象带有必要的信息,并将其与报告文档一起传递给函数。希望这会有所帮助。
You should be able to simply create a new ConnectionInfo object with the necessary info and pass it into the function along with the report document. Hope this helps.
这篇关于动态更改Crystal Report的连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!