Web Crystal 报表生成数据库登录失败 [英] Web Crystal reports produce Database logon failed

查看:20
本文介绍了Web Crystal 报表生成数据库登录失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在本地机器上开发了一个 Crystal 报表,我可以使用 .ExportToHttpResponse 在我的本地网站上呈现报表,但是一旦我将代码移动到生产服务器,我就会收到数据库登录失败"错误.我基本上使用 .XML 数据集文件作为在本地开发报告的源.这是我的代码:

I developed a Crystal report on my local machine and I can render the report on my local website using .ExportToHttpResponse but as soon as I move my code to the production server I get a "Database logon failed" error. I basically used a .XML dataset file as the source to develop the report locally. This was my code:

我调用一个方法来渲染报告:

I call a method to render the report:

private void RenderCrystalReports(string rptName, string pdfReportName, DataSet dataForReport)
  {
    string reportPath = Server.MapPath(@"App_DataReports" + rptName);
    ReportDocument report = new ReportDocument();
    report.Load(reportPath, OpenReportMethod.OpenReportByTempCopy);
    SetDBLogonForReport(ref report);
    report.SetDataSource(dataForReport);
    SetDBLogonForReport(ref report);
    report.ExportToHttpResponse(ExportFormatType.PortableDocFormat, System.Web.HttpContext.Current.Response, true, pdfReportName);
  }

并获取我拥有的数据集:

and to get the dataset I have:

private DataSet GetReportData(DateTime from, DateTime to, int userID, bool totalsOnly)
  {
    DataTable Job = _reportRepository.GetAccountHistoryJob(from, to, userID, totalsOnly);
    Job.TableName = "Accounts";
    DataSet ds = new DataSet("AccountsDS");
    ds.Tables.AddRange(new DataTable[] { Job });
    return ds;
  }

就我而言,下面的方法应该可以帮助我连接到远程 SQL 服务器:

and as far as I am concerned the method below is what is supposed to help me to connect to the remote SQL server:

private void SetDBLogonForReport(ref ReportDocument reportDocument)
  {
    SqlConnectionStringBuilder builder = new SqlConnectionStringBuilder(ConfigurationManager.ConnectionStrings["MyConnectionString"].ToString());
    ConnectionInfo connectionInfo = new ConnectionInfo();
    connectionInfo.DatabaseName = builder.InitialCatalog;
    connectionInfo.UserID = builder.UserID;
    connectionInfo.Password = builder.Password;
    connectionInfo.ServerName = builder.DataSource;

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

为什么这不起作用?我刚刚发现即使在本地也使用了 .xml 数据集,那么如何使用我得到的 sql 数据集呢?

Why does this not work? I just figured out that even locally the .xml dataset is used so how do I use the sql dataset that I get?

推荐答案

这是对我有用的代码:

public ActionResult TestReport() {
  RenderCrystalReports("demoRPT.rpt", "Some Report", GetReportDs());
  return View();
}


#region Private methods
private void RenderCrystalReports(string rptName, string pdfReportName, DataSet dataForReport)
{
  string reportPath = Server.MapPath(@"Reports" + rptName);
  ReportDocument report = new ReportDocument();
  report.Load(reportPath, OpenReportMethod.OpenReportByTempCopy);

  DataTable jobTable = GetReportTable();
  report.Database.Tables[0].SetDataSource(jobTable);

  report.ExportToHttpResponse(ExportFormatType.PortableDocFormat, System.Web.HttpContext.Current.Response, true, pdfReportName);

}

private DataTable GetReportTable()
{
  DataTable Job = GetReportData();
  Job.TableName = "Accounts";
  return Job;
}

private DataSet GetReportDs()
{
  DataTable Job = GetReportData();
  Job.TableName = "Accounts";
  DataSet ds = new DataSet("AccountsDS");
  ds.Tables.AddRange(new DataTable[] { Job });
  return ds;
}

private DataTable GetReportData()
{
  DataTable table = new DataTable();
  table.Columns.Add("Date", typeof(DateTime));
  table.Rows.Add(DateTime.Now);
  return table;
}
#endregion

这篇关于Web Crystal 报表生成数据库登录失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆