c# Crystal Reports call

本文关键字:call Reports Crystal | 更新日期: 2023-09-27 18:07:07

我已经到处看了,很多研究,测试了许多不同的人的代码,仍然有同样的问题试图打开一个水晶报告发送两个参数到报告。

每次打开都是空白。我在查看器上点击刷新,它询问参数并正常打开。

我的代码列在下面。我要赶截止日期了。如果有人能看看这个并帮助我,我会很感激的。研究了好几个小时后我迷路了。

        ReportDocument cryRpt = new ReportDocument();
        cryRpt.Load(@"C:'reports'myReport.rpt");
        cryRpt.SetDatabaseLogon("", "", "MyServer", "MyDB");
        ConnectionInfo c = new ConnectionInfo();
        c.ServerName = "MyServer";
        c.IntegratedSecurity = true;
        c.DatabaseName = "MyDB";
        CrystalDecisions.CrystalReports.Engine.Tables CrTables = cryRpt.Database.Tables;
        foreach (CrystalDecisions.CrystalReports.Engine.Table CrTable in CrTables)
        {
            CrystalDecisions.Shared.TableLogOnInfo crtableLogoninfo = CrTable.LogOnInfo;
            crtableLogoninfo.ConnectionInfo = c;
            CrTable.ApplyLogOnInfo(crtableLogoninfo);
        }
        cryRpt.SetParameterValue("@parmBeginDate", "01-01-2010");
        cryRpt.SetParameterValue("@parmEndDate", "03-30-2013");
        crystalReportViewer1.ReportSource = cryRpt;
        crystalReportViewer1.Refresh();
        crystalReportViewer1.Show();

c# Crystal Reports call

如果您的报表有一个或多个子报表,就会发生这种情况。尝试重构将报表连接设置为方法。然后,您可以枚举子报表集合,并为每个子报表设置连接信息。我也喜欢直接设置ConnectionInfo对象。

foreach (ReportDocument subReport in report.Subreports)
{
    SetDatabaseConnectionInformation(subReport);
}
private void SetDatabaseConnectionInformation(ReportDocument report)
{
    ConnectionInfo connectionInfo = new ConnectionInfo();
    connectionInfo.AllowCustomConnection = true;
    connectionInfo.Type = ConnectionInfoType.SQL;
    connectionInfo.ServerName = MyServer;
    connectionInfo.IntegratedSecurity = (true or false);
    connectionInfo.UserID = MyUserName;
    connectionInfo.Password = MyPassword;
    foreach (Table table in report.Database.Tables)
    {
        TableLogOnInfo tableLogOnInfo = table.LogOnInfo;
        tableLogOnInfo.ConnectionInfo = connectionInfo;
        table.ApplyLogOnInfo(tableLogOnInfo);
    }
}

另外,如果您有多个数据源,您需要设置InternalConnectionInfo类来单独设置不同数据源的连接信息。

答案是在将报告加载到crystalreportviewer之后声明我的参数。然后刷新。在那之后,它工作得很好。谢谢你的帮助。