0

在这里,我使用以下代码在加载 Crystal Report 时输入单个数据库的登录详细信息。

rpt.Load(reportPath);
ConnectionInfo connectionInfo = new ConnectionInfo();
connectionInfo.DatabaseName = "Northwind";
connectionInfo.UserID = "user";
connectionInfo.Password="user123";
SetDBLogonForReport(connectionInfo,rpt);
CrystalReportViewer1.ReportSource = rpt;

 private void SetDBLogonForReport(ConnectionInfo connectionInfo, ReportDocument reportDocument)
{
    Tables tables = reportDocument.Database.Tables;
    foreach (CrystalDecisions.CrystalReports.Engine.Table table in tables)
    {
        TableLogOnInfo tableLogonInfo = table.LogOnInfo;
        tableLogonInfo.ConnectionInfo = connectionInfo;
        table.ApplyLogOnInfo(tableLogonInfo);
    }
}

但是我如何在此代码中输入 2 个数据库的名称及其登录详细信息以加载 Crystal Report...

4

2 回答 2

0

SetDBLogonForReport检查_If (reportDocument.Database.Tables.Item(i).Name == "TableName")

然后申请

tableLogonInfo.ConnectionInfo.ServerName = "AltetrnativeName"
tableLogonInfo.ConnectionInfo.DatabaseName= "AltetrnativeDBName"
tableLogonInfo.ConnectionInfo.ReportUser= "AltetrnativeUser"
tableLogonInfo.ConnectionInfo.Password= "AltetrnativePsw"

如果没有,则使用您传递的 connectionInfo 。这是为了解释的目的,您也可以将 AlternativeConnectionInfo 作为参数传递。

这种方式允许您通过表格将任意数量的 BD 连接到您的报告。

如果不清楚请告诉我。我很乐意以更详细的方式解释。

于 2012-06-12T14:38:59.013 回答
0

从您的帖子中,我添加了一些示例代码

rpt.Load(reportPath);
ConnectionInfo connectionInfo = new ConnectionInfo();
connectionInfo.DatabaseName = "Northwind";
connectionInfo.UserID = "user";
connectionInfo.Password="user123";

ConnectionInfo altConnectionInfo = new ConnectionInfo(); 
altConnectionInfo.DatabaseName = "altDataBase";
altConnectionInfo.UserID = "atlUser";
altConnectionInfo.Password="123user123";

SetDBLogonForReport(connectionInfo, altConnectionInfo, rpt);
CrystalReportViewer1.ReportSource = rpt;

 private void SetDBLogonForReport(ConnectionInfo connectionInfo, ConnectionInfo altConnectionInfo ReportDocument reportDocument)
{
    Tables tables = reportDocument.Database.Tables;
    foreach (CrystalDecisions.CrystalReports.Engine.Table table in tables)
    {
        TableLogOnInfo tableLogonInfo = table.LogOnInfo;
        //you can add as much tables as you want but associated with the same connectionInfo
        if (table.Name == "SomeTable") { tableLogonInfo.ConnectionInfo = altConnectionInfo; }
        //if you have multiple connections, you could even use switch structure depending on the table name.
        else { tableLogonInfo.ConnectionInfo = connectionInfo; }
        //unfortunately, this uses table name property, so you must specify any table name depending on the connection you want to associate with.  

        table.ApplyLogOnInfo(tableLogonInfo);
    }
}

要使用 Crystal Report,SetDBLogonForReport 允许您向每个表添加一个 connectionInfo。这就是 CR 的工作方式。我无法找到另一种方法(我不喜欢使用硬编码表名)

希望这可以帮助!

于 2012-06-14T12:47:25.463 回答