我知道这有问题,因为我发现了很多关于此的线程,但对我没有任何帮助,我快疯了!
生成报告时,我必须更改登录信息。但是 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
好吧,现在我被困住了,真的需要你的帮助!谢谢