繁体   English   中英

Crystal Reports无法打开连接-在ReportDocument.Load阶段。

[英]Crystal Reports failed to open connection - at ReportDocument.Load stage.

我目前在Crystal Reports方面遇到问题,因此我们不断收到此错误:

PDF error: Failed to open the connection. Failed to open the connection. 4250901051516FA90CB6FED9F270A2A7 11952_8824_{CD4A46B0-F17C-4675-94B3-5F200A267CCF}.rpt  InnerException: Failed to open the connection. Failed to open the connection. 4250901051516FA90CB6FED9F270A2A7 11952_8824_{CD4A46B0-F17C-4675-94B3-5F200A267CCF}.rpt

本质上,我们已经创建了Windows Task Scheduler运行的Crystal Reports处理器服务。 该服务运行报告,将报告导出为PDF,将PDF报告发送为电子邮件,并存储在中央位置。

该服务对于某些报告可以正常工作-但是,该服务在调试模式下不能很好地工作,并不断向我们显示此错误。 即使对于似乎正常工作的报告,也会发生此错误。

此异常总是发生在Export()方法阶段的末尾。 但是,我注意到,当ReportDocument.HasRecords属性指示内部异常时,此异常将在ReportDocument.Load(thisCrystalDocument)处引起丑陋的印象。

我的设置如下:VS2013 Professional,Crystal Reports 2011,Crystal Reports 2011 SP8更新,用于32位.NET Framework(v15)的SAP Crystal Reports运行时引擎和32位SQL Server 2008 R2。 这不是一个ASP.NET项目。

一些代码片段:

public CrystalReportGenerator(string reportLocation, string crystalParameters)
    {
      this.reportDocument = new ReportDocument();
      if (!File.Exists(reportLocation))
      {
        throw new Exception(String.Format("Report at '{0}' does not exist.", reportLocation));
      }

      // Cache report file
      var cacheFolder = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData) + CrystalReportGenerator.cacheSubfolder);
      if (!Directory.Exists(cacheFolder))
      {
        Directory.CreateDirectory(cacheFolder);
      }
      var cachedLocation = Path.Combine(cacheFolder, reportLocation.GetMd5Sum()) + ".rpt";
      if (!File.Exists(cachedLocation))
      {
        File.Copy(reportLocation, cachedLocation, false);
      }

      // Generate report
      this.reportDocument.Load(cachedLocation);
      this.SetParameters(crystalParameters);

      // Override existing database login for report
      this.reportDocument.SetDatabaseLogon(CrystalReportGenerator.sqlUser, CrystalReportGenerator.sqlPassword, CrystalReportGenerator.sqlHost, CrystalReportGenerator.sqlDatabase, false);

      // Override existing database login for subreport(s)
      if (this.reportDocument.Subreports.Count > 0)
      {
        for (int i = 0; i < this.reportDocument.Subreports.Count; i++)
        {
          this.reportDocument.Subreports[i].SetDatabaseLogon(CrystalReportGenerator.sqlUser, CrystalReportGenerator.sqlPassword, CrystalReportGenerator.sqlHost, CrystalReportGenerator.sqlDatabase, false);
        }
      }
    }

private static void GenerateReport(string reportLocation, string reportTitle, string reportParameters, string outputLocation = null, string outputFormat = null, MailMessage message = null)
    {
      var isTempFile = false;

      // Generate report
      using (var generator = new CrystalReportGenerator(reportLocation, reportParameters))
      {
        if (String.IsNullOrWhiteSpace(outputLocation))
        {
          outputLocation = Path.GetTempFileName();
          isTempFile = true;
        }
        else
          outputLocation = outputLocation.ConvertDateTimeReference();

        generator.Export(outputLocation, outputFormat);

        // Send mail message
        if (message != null)
        {
          using (var attachment = new Attachment(outputLocation)
          {
            Name = String.Format("{0}.{1}", reportTitle.ConvertDateTimeReference(), outputFormat.Replace("-DATA", "").ToLower())
          })
          {
            message.Attachments.Add(attachment);
            Emailer.SendMailMessage(message);
          }
        }

        // Clean up temp file
        if (isTempFile && File.Exists(outputLocation))
        {
          File.Delete(outputLocation);
        }
      }
    }

private void Export(string outputLocation, string outputFormat)
    {
      ExportOptions exportOptions = this.reportDocument.ExportOptions;
      exportOptions.ExportDestinationType = ExportDestinationType.DiskFile;
      exportOptions.DestinationOptions = new DiskFileDestinationOptions
      {
        DiskFileName = outputLocation
      };

      switch (outputFormat.ToUpper())
      {
        case "XLS-DATA":
          {
            exportOptions.ExportFormatType = ExportFormatType.ExcelRecord;
            exportOptions.FormatOptions = new ExcelFormatOptions();
            break;
          }
        case "XLS":
        case "XLSX":
          {
            exportOptions.ExportFormatType = ExportFormatType.ExcelWorkbook;
            exportOptions.FormatOptions = new ExcelFormatOptions();
            break;
          }
        case "PDF":
        default:
          {
            exportOptions.ExportFormatType = ExportFormatType.PortableDocFormat;
            exportOptions.FormatOptions = new PdfRtfWordFormatOptions();
            break;
          }
      }
      this.reportDocument.Export();
    }

除此之外,我注意到的另一件事是ReportDocument.HasRecords总是引发异常。

如果这个问题听起来有点愚蠢,我事先表示歉意。 这是我第一次进行Crystal Reports尝试,我非常感谢那些在这方面比我经验丰富的人的智慧。 谢谢。

在对每个MyReportToRun.rpt文件进行进一步检查之后,我重置了数据库连接字符串并刷新了每个报告,然后再次运行我的应用程序。 事实证明,这很有用,因为它在以后工作了很多。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM