0

我知道这有问题,因为我发现了很多关于此的线程,但对我没有任何帮助,我快疯了!

生成报告时,我必须更改登录信息。但是 ApplyLogOnInfo 方法总是带回坏数据。我尝试连接到 ODBC MySQL 数据库。

这是我的代码:

ReportDocument myReport = new ReportDocument();
myReport.Load("myReportPath");

ParameterField myParamFieldId = myReport.ParameterFields["param1"];
ParameterDiscreteValue param = new ParameterDiscreteValue();
param.Value = "param1";
myParamFieldId.CurrentValues.Add(param);

ParameterField myParamFieldNumRevision = myReport.ParameterFields["param2"];
ParameterDiscreteValue paramNumRevision = new ParameterDiscreteValue();
param.Value = "param2";
myParamFieldNumRevision.CurrentValues.Add(param);

ApplyLogOnInfo(myReport);

try
{
    myReport.ExportToHttpResponse(ExportFormatType.PortableDocFormat, Response, true, "Rapport");
    myReport.Close();
}
catch (Exception ex)
{
    throw ex;
}
finally
{
    myReport.Dispose();
}


private void ApplyLogOnInfo(ReportDocument rpt)
{
    ConnectionInfo connInfo = new ConnectionInfo();
    connInfo.ServerName = "test";
    connInfo.DatabaseName = "test";
    connInfo.UserID = "test";
    connInfo.Password = "test";
    connInfo.Type = ConnectionInfoType.CRQE;
    connInfo.IntegratedSecurity = false;

    foreach (CrystalDecisions.CrystalReports.Engine.Table table in rpt.Database.Tables)
    {
        TableLogOnInfo logonInfo = table.LogOnInfo;
        logonInfo.ConnectionInfo = connInfo;
        table.ApplyLogOnInfo(logonInfo); // Here, old values are brought back
    }


    // Idem on Sub-reports
    foreach (ReportDocument sousRpt in rpt.Subreports)
    {
        ReportDocument rptSub = rpt.OpenSubreport(sousRpt.Name);
        foreach (CrystalDecisions.CrystalReports.Engine.Table table in rptSub.Database.Tables)
        {
            TableLogOnInfo logonInfo = table.LogOnInfo;
            logonInfo.ConnectionInfo = connInfo;
            table.ApplyLogOnInfo(logonInfo);
        }
    }
}

我看到这可能是由于子报告和参数造成的。没有办法让它工作。

我也试过:

rpt.DataSourceConnections[0].SetConnection("test", "test", "test", "test");
// But DataSourceConnections[0].Attributes and DataSourceConnections[0].LogonProperties still store values from the old connection, and doesn't work. I tried to clear it does'nt apply.

// I tried to pass parameters like this
myReport.SetParameterValue("param1", "test");
myReport.SetParameterValue("param1", "test");

// I tried with dataset and fill method

好吧,现在我被困住了,真的需要你的帮助!谢谢

4

1 回答 1

0

我意识到这是几个月后的事,你可能已经解决了。在将我们的 Crystal Reports 从 2008 年升级到 2012 年时,我注意到我们如何传递参数和登录凭据存在问题。总的来说,一切都没有像我希望的那样工作。

我决定最好编写一个在 2008 年和 2012 年都兼容的新函数,并且总体上更加灵活。它需要查看器、源、报告路径和参数来加载它。

我刚开始在 2008 年的生产环境中使用它来制作一些效果很好的报告。如果它不能完全按照您对可能来自或可能不来自不同 ODBC 登录凭据的子报告的工作方式工作,我建议您进一步修改它。

    public void LoadCrystalReportViewer(CrystalReportViewer crystalReportViewer, CrystalReportSource crystalReportSource, string reportFilePath, Dictionary<string, object> parameters)
    {
        crystalReportSource.ReportDocument.FileName = reportFilePath;
        crystalReportSource.ReportDocument.Load(reportFilePath);
        crystalReportSource.ReportDocument.Database.Tables[0].LogOnInfo.ConnectionInfo.Password = "PASSWORD";
        crystalReportSource.ReportDocument.Database.Tables[0].ApplyLogOnInfo(crystalReportSource.ReportDocument.Database.Tables[0].LogOnInfo);

        foreach (KeyValuePair<string, object> parameter in parameters)
        {
            if (crystalReportSource.ReportDocument.ParameterFields[parameter.Key] != null)
            {
                crystalReportSource.ReportDocument.SetParameterValue(parameter.Key, parameter.Value);
            }
        }

        crystalReportSource.EnableCaching = true;
        crystalReportSource.CacheDuration = 300;
        crystalReportViewer.ReportSource = crystalReportSource;
        crystalReportViewer.ReuseParameterValuesOnRefresh = true;
        crystalReportViewer.EnableParameterPrompt = false;
        crystalReportViewer.LogOnInfo.Add(crystalReportSource.ReportDocument.Database.Tables[0].LogOnInfo);

        foreach (ParameterField parameterField in crystalReportViewer.ParameterFieldInfo)
        {
            parameterField.DefaultValues = crystalReportSource.ReportDocument.ParameterFields[parameterField.Name].CurrentValues;
        }

        crystalReportViewer.Visible = true;
    }
于 2013-09-18T16:05:42.997 回答