簡體   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