SysRunLog objSysRunLog = SysRunLog.GetInstance(); private Int16 daynum = Convert.ToInt16(ConfigurationSettings.AppSettings["DaysNum"].Trim()); private DateTime beginDate ; private DateTime endDate; public void Run() { beginDate = DateTime.Now.AddDays(- daynum); endDate = DateTime.Now; //objSysRunLog.WriteLog("准備發郵件……"); string emailSubject = ""; string emailBody = ""; DataSet ds = new DataSet(); //emailSubject = beginDate.ToString("yyyy年MM月dd日") + "至" + endDate.ToString("yyyy年MM月dd日") + "未注冊客戶服務結算辦法信息表(試行)"; emailSubject = endDate.ToString("yyyy年MM月dd日") + "未注冊用戶救援情況"; ProcessFile process = new ProcessFile(); string basePath = AppDomain.CurrentDomain.BaseDirectory; string excel = basePath + @"temp\" + DateTime.Now.ToString("yyyymmddhhmm") + ".xls"; //objSysRunLog.WriteLog(excel); try { ds = WriteLetter2(); } catch (Exception e) { objSysRunLog.WriteLog("查詢失敗……" + e); } EMail email = new EMail(); //如果有數據,將數據寫入EXCEL if (ds == null) { emailBody = "連接數據庫失敗!"; } else if (ds.Tables[0].Rows.Count > 0) { try { //構建用戶數據導入的標准DataGrid,並綁定數據; System.Web.UI.WebControls.DataGrid dg = new System.Web.UI.WebControls.DataGrid(); dg.DataSource = ds.Tables[0]; dg.DataBind(); //將數據寫入EXCEL中 process.ExportDataGridToExcel(dg, excel); emailBody = beginDate.ToString("yyyy年MM月dd日") + ConfigurationSettings.AppSettings["Hour2"].Trim() + "時" + ConfigurationSettings.AppSettings["Minute2"].Trim() + "分至" + endDate.ToString("yyyy年MM月dd日") + ConfigurationSettings.AppSettings["Hour2"].Trim() + "時" + ConfigurationSettings.AppSettings["Minute2"].Trim() + "分" + "時間內數據詳見附件。(請不要回復此郵件)"; email.Attach = excel; } catch (Exception e) { objSysRunLog.WriteLog("寫入EXCEL失敗……" + e.ToString()); } } else { emailBody = beginDate.ToString("yyyy年MM月dd日") + ConfigurationSettings.AppSettings["Hour2"].Trim() + "時" + ConfigurationSettings.AppSettings["Minute2"].Trim() + "分至" + endDate.ToString("yyyy年MM月dd日") + ConfigurationSettings.AppSettings["Hour2"].Trim() + "時" + ConfigurationSettings.AppSettings["Minute2"].Trim() + "分" + "時間內無數據。(請不要回復此郵件)"; } try { email.To = ConfigurationSettings.AppSettings["MailTo2"].Trim(); email.Subject = emailSubject; email.Body = emailBody; email.send(); objSysRunLog.WriteLog("郵件發送成功:"); } catch (Exception e) { objSysRunLog.WriteLog("郵件發送失敗:" + e.Message); } } public DataSet WriteLetter2() { string result = ""; string connectString = System.Configuration.ConfigurationSettings.AppSettings["connString"]; OracleConnection myConn = new OracleConnection(connectString); OracleCommand myCMD = new OracleCommand(); myCMD.CommandType = CommandType.StoredProcedure; myCMD.CommandText = @"PK_EXPORT.P_NOTREG_USER_INTRADAY_INFO"; myCMD.Parameters.Add("V_DAYS_NUM", OracleType.Number).Value = daynum; myCMD.Parameters.Add("V_RESULT", OracleType.Cursor); myCMD.Parameters["V_RESULT"].Direction = ParameterDirection.Output; myCMD.Parameters.Add("RETURN_CODE", OracleType.Int32); myCMD.Parameters["RETURN_CODE"].Direction = ParameterDirection.Output; myCMD.Parameters.Add("RETURN_TEXT", OracleType.VarChar, 100); myCMD.Parameters["RETURN_TEXT"].Direction = ParameterDirection.Output; myConn.Open(); myCMD.Connection = myConn; OracleDataAdapter myAdapter = new OracleDataAdapter(myCMD); DataSet ds = new DataSet(); try { myAdapter.Fill(ds); } catch (Exception exp) { result = "連接數據庫時出錯:" + exp.ToString(); ds = null; } finally { if (ConnectionState.Open == myConn.State) { myConn.Close(); } } return ds; }