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

时间:2016-02-02 14:21:14

标签: c# .net crystal-reports

我目前面临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任务计划程序运行的Crystal Reports处理器服务。此服务运行报告,将报告导出为PDF,将PDF报告作为电子邮件发送,并存储在中央位置。

此服务适用于某些报告 - 但是,该服务在调试模式下无法正常工作,并且不断向我们提供此错误。即使对于似乎正常工作的报告,也会发生此错误。

此异常始终发生在Export()方法阶段的末尾。但是,我注意到,当ReportDocument.HasRecords属性指示内部异常时,此异常将在ReportDocument.Load(thisCrystalDocument)中隐藏其丑陋的头部。

我的设置如下: VS2013 Professional,Crystal Reports 2011,Crystal Reports 2011 SP8更新,用于.NET Framework 32位(v15)的SAP Crystal Reports运行时引擎和SQL Server 2008 R2 32位。这不是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,我非常感谢那些在这方面比我更有经验的人的智慧。谢谢。

1 个答案:

答案 0 :(得分:0)

after some further examination of each MyReportToRun.rpt file, I resetted the database connection strings and refreshed each report, and ran my application again. This proved to be helpful as it worked a great deal after.