1
我想問爲什麼服務沒有從sql獲取數據,應用程序在控制檯應用程序中正常工作,但是當我將其粘貼到服務中時,它不起作用。我從網上搜索了很多東西,但沒找到。你可以幫我嗎。在代碼中,我嘗試保存在Excel文件sql查詢中,然後發送給郵件。從SQL讀取數據到服務C#
謝謝。
<pre><code>using OfficeOpenXml;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Mail;
using System.ServiceProcess;
using System.Text;
using System.Threading.Tasks;
namespace Nerma_Mail_Reporter_Service
{
public partial class Service1 : ServiceBase
{
public Service1()
{
InitializeComponent();
}
System.Timers.Timer createOrderTimer;
private string folderPath = @"c:\CEMAL";
static string current = DateTime.Now.ToShortTimeString();
static DataTable dtService;
static DataTable dtMailfrom;
static SqlDataAdapter daService;
static SqlDataAdapter daMailfrom;
static DataTable dtDay;
static SqlDataAdapter daDay;
static int repID;
protected override void OnStart(string[] args)
{
if (!System.IO.Directory.Exists(folderPath))
System.IO.Directory.CreateDirectory(folderPath);
EventLog.WriteEntry("start Calisti", EventLogEntryType.Information);
createOrderTimer = new System.Timers.Timer();
createOrderTimer.Interval = 6000;
createOrderTimer.Enabled = true;
createOrderTimer.AutoReset = true;
createOrderTimer.Start();
createOrderTimer.Elapsed += new System.Timers.ElapsedEventHandler(Gonder_Elapsed);
}
protected override void OnContinue()
{
EventLog.WriteEntry("OnContinue Calisti", EventLogEntryType.Information);
createOrderTimer.Elapsed += new System.Timers.ElapsedEventHandler(Gonder_Elapsed);
}
protected override void OnStop()
{
EventLog.WriteEntry("Nerma Danismanlik Servisi Durdurulmustur...", EventLogEntryType.SuccessAudit);
}
private void Gonder_Elapsed(object sender, EventArgs e)
{
using (SqlConnection conn = new SqlConnection("Server=CEMAL;UID=sa;pwd=123 ;Database=MailReporter"))
{ EventLog.WriteEntry("Gonder Calisti", EventLogEntryType.Information);
conn.Open();
daService = new SqlDataAdapter("select * from SENDSERVICE", conn);
dtService = new DataTable();
daService.Fill(dtService);
foreach (DataRow dr in dtService.Rows)
{
EventLog.WriteEntry("dtservice Calisti", EventLogEntryType.Information);
SaveToExcel(18);
SendMail(18);
EventLog.WriteEntry("Mail Gitt");
DateTime start = Convert.ToDateTime(dr["STARTRUNTIME"]);
DateTime end = Convert.ToDateTime(dr["ENDRUNTIME"]);
DateTime next = Convert.ToDateTime(dr["NEXTRUNTIME"]);
bool gittiMi = Convert.ToBoolean(dr["GITTIMI"]);
DateTime now = DateTime.Now;
// string format = "HH:mm:ss";
string format = "HH:mm";
EventLog.WriteEntry("DTService BItti", EventLogEntryType.Information);
now.ToString(format);
int ServiceID = Convert.ToInt32(dr["ID"]);
int result = DateTime.Compare(next, end);
bool isDayly = Convert.ToBoolean(dr["isDayly"]);
if (result <= 0 && gittiMi == true)
{
using (SqlCommand cmd = new SqlCommand("update SENDSERVICE set GITTIMI=false)", conn))
{
cmd.ExecuteNonQuery();
}
}
EventLog.WriteEntry("isdayly kontrol gecti calisacak", EventLogEntryType.Information);
if (isDayly)
{ EventLog.WriteEntry("isdayly kontrol gecti Calisti", EventLogEntryType.Information);
//Devam Eden Islemler
if (result <= 0 && next.ToString(format) == now.ToString(format) && gittiMi == false)
{
EventLog.WriteEntry("isDayly Calisti", EventLogEntryType.Information);
int ID = 0;
int every = 0;
{
using (daDay = new SqlDataAdapter("select * from Dayly", conn))
{
dtDay = new DataTable();
daDay.Fill(dtDay);
foreach (DataRow day in dtDay.Rows)
{
if (Convert.ToInt32(day["SERVICEID"]) == ServiceID)
{
ID = Convert.ToInt32(day["ID"]);
every = Convert.ToInt32(day["EVERYisDayly"]);
using (SqlCommand insert = new SqlCommand("update SENDSERVICE set LASTRUNTIME='" + DateTime.Now.ToShortDateString() + "',NEXTRUNTIME='" + DateTime.Now.AddDays(every) + "' where ID=" + ServiceID + "", conn))
{
insert.ExecuteNonQuery();
}
}
}
EventLog.WriteEntry("Save to Excel Baslayacak", EventLogEntryType.Information);
SaveToExcel(ServiceID);
bool isSend = SendMail(ServiceID);
using (SqlCommand cmd = new SqlCommand("update SENDSERVICE set GITTIMI='" + isSend + "' where ID=" + ServiceID + "", conn))
{ EventLog.WriteEntry("IsSend Calisti", EventLogEntryType.Information);
cmd.ExecuteNonQuery();
}
}
}
}
}
}
}
EventLog.WriteEntry("gonder bitti");
}
public bool SendMail(int ServiceID)
{
try
{
EventLog.WriteEntry("SENDMAil Calisti", EventLogEntryType.Information);
using (SqlConnection conn = new SqlConnection("Server=CEMAL;UID=sa;pwd=123 ;Database=MailReporter"))
{
daMailfrom = new SqlDataAdapter("select * from MAILFROM where SERVICEID=" + ServiceID + "", conn);
dtMailfrom = new DataTable();
daMailfrom.Fill(dtMailfrom);
daService = new SqlDataAdapter("select * from SENDSERVICE where ID=" + ServiceID + "", conn);
dtService = new DataTable();
daService.Fill(dtService);
foreach (DataRow mailfrom in dtMailfrom.Rows)
{
foreach (DataRow service in dtService.Rows)
{
string smtpAddress = mailfrom["SMTP"].ToString();
int portNumber = Convert.ToInt32(mailfrom["PORT"]);
bool enableSSL = Convert.ToBoolean(mailfrom["ENABLESSL"]);
string emailFrom = service["EMAILFROM"].ToString();
string password = mailfrom["PASS"].ToString();
string emailTo = service["EMAILTO"].ToString();
//string subject = service["SUBJECT"].ToString();
string subject = DateTime.Now.ToLongTimeString();
string body = service["BODY"].ToString();
string Attachement = service["RAPORADI"].ToString();
using (MailMessage mail = new MailMessage())
{
mail.From = new MailAddress(emailFrom);
mail.To.Add(emailTo);
mail.Subject = subject;
mail.Body = body;
mail.IsBodyHtml = true;
mail.Attachments.Add(new Attachment(@"C:\CEMAL\" + Attachement + ".xlsx"));
using (SmtpClient smtp = new SmtpClient(smtpAddress, portNumber))
{
smtp.Credentials = new NetworkCredential(emailFrom, password);
smtp.EnableSsl = enableSSL;
smtp.Send(mail);
return true;
// EventLog.WriteEntry("Mail GOnderildi Calisti", EventLogEntryType.Information);
}
}
}
}
}
}
catch(Exception){}
return false;
}
public void SaveToExcel(int ServiceID)
{
EventLog.WriteEntry("Save to Excel Calisti", EventLogEntryType.Information);
string RaporAdi = "";
using (SqlConnection conSave = new SqlConnection("Server=CEMAL;Database=MailReporter;user id=sa;pwd=123"))
{
conSave.Open();
SqlDataAdapter daRer = new SqlDataAdapter("select * from NERRAPOR where SERVICEID=" + ServiceID + "", conSave);
DataTable dtRep = new DataTable();
daRer.Fill(dtRep);
foreach (DataRow dr in dtRep.Rows)
{
if (Convert.ToInt32(dr["SERVICEID"]) == ServiceID)
{
RaporAdi = dr["RAPORADI"].ToString();
string SqlCumle = dr["SQLCUMLE"].ToString();
SqlDataAdapter cumle = new SqlDataAdapter(SqlCumle, "Server=CEMAL;Database=MAS_UYG;USER ID=SA;PWD=123;");
DataTable dtcumle = new DataTable();
cumle.Fill(dtcumle);
using (ExcelPackage pck = new ExcelPackage(new FileInfo(@"C:\CEMAL\" + RaporAdi + ".xlsx")))
{
//if (File.Exists(@"C:\CEMAL\" + RaporAdi + ".xlsx"))
//{
// // ws = pck.Workbook.Worksheets.Copy(DateTime.Now.ToShortDateString(), @"C:\CEMAL\" + RaporAdi + "" + DateTime.Now.ToShortTimeString() + ".xlsx");
// File.Delete(@"C:\CEMAL\" + RaporAdi + ".xlsx");
// // ExcelPackage pcks = new ExcelPackage(new FileInfo(@"C:\CEMAL\" + RaporAdi + ".xlsx"));
//}
ExcelWorksheet ws = pck.Workbook.Worksheets.Add(RaporAdi + " " + DateTime.Now.TimeOfDay);
ws.Cells["A1"].LoadFromDataTable(dtcumle, true);
pck.Save();
EventLog.WriteEntry("Save bitti Calisti", EventLogEntryType.Information);
}
}
}
}
}
}
}
<pre><code>
我們可以幫忙,我們只需要看代碼。你如何試圖通過你的服務訪問數據庫?你試過什麼了? –
您不必問問權限即可提出問題。繼續併發布相關的代碼,描述你有什麼跡象表明它不起作用(包括任何特定的錯誤消息和異常信息)等。 – David
它在EventLog.WriteEntry(「dtservice Calisti」,EventLogEntryType.Information)中循環。 –