Crystal Reports和数据在运行时绑定

时间:2009-09-17 15:10:03

标签: crystal-reports

我现在已经苦苦挣扎了4天了。 我有一个非常简单的水晶报告(我只是为了概念验证而使用它)。报告绑定到数据库,我只显示数据库中一个表的一个字段。没有子报告。它是使用Crystal Reports 2008创建的。我需要在我的.Net MVC Web应用程序中显示此报告但我需要能够从此应用程序更改数据库连接信息。将用于具有相同表结构的不同数据库。 所以我创建了一个标准的Web表单,然后将CrystalReportViewer和CrystalReportSource拖到它上面。

这是我的代码:

protected void Page_Load(object sender, EventArgs e)
    {
        this.CrystalReportSource1.EnableCaching = false;
        this.CrystalReportSource1.ReportDocument.Load(@"C:\ReportName.rpt");

        //1)  I get the data connection variables from my app - this part works well  
              and is irrelevant in this case.

 //2) Once I have the data I need to apply it to the connection of the report
 ConnectionInfo crConnection = new ConnectionInfo();
 crConnection.UserID = userID;
 crConnection.ServerName = datasource;
 crConnection.DatabaseName = "";
 crConnection.Password = password;


 AssignConnectionInfo(CrystalReportSource1.ReportDocument,crConnection);

 CrystalReportSource1.ReportDocument.DataSourceConnections[0].SetConnection 
 (crConnection.ServerName, crConnection.DatabaseName, false);

CrystalReportSource1.ReportDocument.SetDatabaseLogon(crConnection.UserID, 
crConnection.Password, crConnection.ServerName, crConnection.DatabaseName);


CrystalReportViewer1.ReportSource = CrystalReportSource1.ReportDocument;
CrystalReportViewer1.RefreshReport();

 }//close the page load function

这是AssignConnectionInfo函数:

private void AssignConnectionInfo(ReportDocument document,ConnectionInfo crConnection)
    {
        foreach (CrystalDecisions.CrystalReports.Engine.Table table in document.Database.Tables)
        {
            TableLogOnInfo logOnInfo = table.LogOnInfo;
            if (logOnInfo != null)
            {
                table.ApplyLogOnInfo(table.LogOnInfo);
                table.LogOnInfo.TableName = table.Name;
                table.LogOnInfo.ConnectionInfo.UserID = crConnection.UserID;
                table.LogOnInfo.ConnectionInfo.Password = crConnection.Password;
                table.LogOnInfo.ConnectionInfo.DatabaseName = crConnection.DatabaseName;
                table.LogOnInfo.ConnectionInfo.ServerName = crConnection.ServerName;

                CrystalReportViewer1.LogOnInfo.Add(table.LogOnInfo);

            }
        }



    }

所以正在发生的是页面加载和Crystal横幅,工具栏显示,但我在报告中的数据绑定字段是空白的。 你看错了吗?

非常感谢提前

苏珊

2 个答案:

答案 0 :(得分:3)

修改AssignConnectionInfo()以便在设置ApplyLogOnInfo()的属性后调用ConnectionInfo,如下所示:

private void AssignConnectionInfo(ReportDocument document,ConnectionInfo crConnection)
{
    foreach (CrystalDecisions.CrystalReports.Engine.Table table in document.Database.Tables)
    {
        TableLogOnInfo logOnInfo = table.LogOnInfo;
        if (logOnInfo != null)
        {
            table.LogOnInfo.TableName = table.Name;
            table.LogOnInfo.ConnectionInfo.UserID = crConnection.UserID;
            table.LogOnInfo.ConnectionInfo.Password = crConnection.Password;
            table.LogOnInfo.ConnectionInfo.DatabaseName = crConnection.DatabaseName;
            table.LogOnInfo.ConnectionInfo.ServerName = crConnection.ServerName;
            table.ApplyLogOnInfo(table.LogOnInfo);

            CrystalReportViewer1.LogOnInfo.Add(table.LogOnInfo);

        }
    }
}

中提琴!

答案 1 :(得分:2)

而不是加载报表源对象。我认为您唯一需要做的就是创建一个ReportDocument对象,加载报告,使用您拥有的函数设置连接信息,并将查看器传递给查看器。您应该可以省略ReportSource部分:

protected void Page_Load(object sender, EventArgs e)
{
    ReportDocument doc = new ReportDocument();
    doc.Load(@"C:\ReportName.rpt");

    ConnectionInfo crConnection = new ConnectionInfo();
    crConnection.UserID = userID;
    crConnection.ServerName = datasource;
    crConnection.DatabaseName = "";
    crConnection.Password = password;

    AssignConnectionInfo(doc,crConnection);

    CrystalReportViewer1.ReportSource = doc;

 }//close the page load function

AssignConnectionInfo函数可以完成许多其他代码的工作。此外,您不需要以下代码:

CrystalReportViewer1.RefreshReport();

报告应该在页面加载时运行,这样只会导致报告再次运行。希望这可以帮助。