Web Crystal reports produce Database logon failed

2019-07-15 11:33发布

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_Data\Reports\" + 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;
  }

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);
    }
  }

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?

1条回答
做个烂人
2楼-- · 2019-07-15 12:02

This is the code that worked for me:

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
查看更多
登录 后发表回答