繁体   English   中英

Windows服务无法打开连接

[英]Windows service fails to open the connection

我的C#Windows服务自动发送电子邮件。 发送之前,我需要将报告转换为PDF并将其附加为PDF。 当我尝试导出PDF时,在下面的行上出现异常:

cr.ExportToDisk(CrystalDecisions.Shared.ExportFormatType.PortableDocFormat, savePath);

无法打开连接(ODBC)

在Windows窗体应用程序中,相同的代码可以正常运行。 我该如何解决?

public void GenerateReport(string rptpath, int no)
{
  try
  {
    string savePath = ConfigurationManager.AppSettings["Savepath"];
    ReportDocument cr = new ReportDocument();

    cr.Load(rptpath);

    TableLogOnInfo logInfo = new TableLogOnInfo();

    Tables crTables = cr.Database.Tables;

    foreach (Table crTable in crTables)
    {
      logInfo.ConnectionInfo.UserID = "sa";
      logInfo.ConnectionInfo.DatabaseName = "SunVue";
      logInfo.ConnectionInfo.ServerName = "SunVue";
      logInfo.ConnectionInfo.Password = "sa";
      logInfo.ConnectionInfo.IntegratedSecurity = false;
      crTable.ApplyLogOnInfo(logInfo);

      DateTime enddate = DateTime.Today;
      DateTime startdate = enddate.AddDays(-7);
      string reportFormula = string.Empty;

      if (no == 201)
      {
        cr.SetParameterValue("DateRange",startdate.ToString("dd'/'MM'/'yyyy")
                                + " to " + enddate.ToString("dd'/'MM'/'yyyy"));

        reportFormula = "{spilInvNum.OrderDate} in DateTime ("
                        + startdate.Year.ToString() + ","
                        + startdate.Month.ToString() + "," +
                        + startdate.Day.ToString() + ", 00, 00, 00) to DateTime ("
                        + enddate.Year.ToString() + ","
                        + enddate.Month.ToString() + ","
                        + enddate.Day.ToString() + ", 00, 00, 00)";

        //reportFormula = "{spilInvNum.OrderDate} in Date(" + startdate.Year.ToString() + "," + startdate.Month.ToString() + "," + startdate.Day.ToString() + ") " + " to DateTime (" + enddate.Year.ToString() + "," + enddate.Month.ToString() + "," + enddate.Day.ToString() + ") " + " and {spilInvNum.DocState} <> 6 and {spilInvNum.DocType} = 4 and {spilInvNum.AccountID} <> 2592";
        cr.RecordSelectionFormula = reportFormula;
      }
      cr.ExportToDisk(CrystalDecisions.Shared.ExportFormatType.PortableDocFormat, savePath);
      WriteToFile(savePath);
    }
  }
}

如何在OBDC中设置连接。

  1. 在建立ODBC后,您是否尝试过测试连接?
  2. 您如何在报表向导中使用连接? 使用OBDC创建的? -在CR向导上连接时,如果创建了错误的连接,则应提示您。

暂无
暂无

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

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