【发布时间】:2019-04-05 08:24:22
【问题描述】:
所以我在我的程序中创建了服务。我需要这项服务来更新我的表格。它应该像检查短信的发送报告一样工作。
所以在这个服务中,我想从短信网关执行api来获取数据,然后将它们插入到SQL中的日志表中。
我设法在服务中创建了计时器,并进行了一些事件日志更新。所以我的服务还活着,但问题是当我试图用我的 sql 服务器做任何事情时,它可能不会执行任何事情,我真的不知道为什么。
服务:
public partial class Service1 : ServiceBase
{
private int eventId = 1;
DataSet ds = new DataSet();
SqlConnection cs = new SqlConnection("Data Source=lvrabel; Initial Catalog=EagleTest; Integrated Security=TRUE;");
SqlDataAdapter da = new SqlDataAdapter();
public Service1()
{
InitializeComponent();
eventLog1 = new EventLog();
if (!EventLog.SourceExists("MySource"))
{
EventLog.CreateEventSource("MySource", "MyNewLog");
}
eventLog1.Source = "MySource";
eventLog1.Log = "MyNewLog";
}
protected override void OnStart(string[] args)
{
eventLog1.WriteEntry("In OnStart");
Timer timer = new Timer();
timer.Interval = 60000; // 60 seconds
timer.Elapsed += new ElapsedEventHandler(this.GetReports);
timer.Start();
}
protected override void OnStop()
{
eventLog1.WriteEntry("In OnStop.");
}
public void OnTimer(object sender, ElapsedEventArgs args)
{
// TODO: Insert monitoring activities here.
// eventLog1.WriteEntry("Monitoring the System", EventLogEntryType.Information, eventId++);
}
public void GetReports(object sender, ElapsedEventArgs args)
{
//SqlCommand selectTelephone = new SqlCommand("select telephone from Contacts where LASTNAME = 'test'", cs);
//cs.Open();
//SqlDataAdapter da = new SqlDataAdapter(selectTelephone);
//DataTable dt = new DataTable();
//da.Fill(dt);
//foreach (DataRow dr in dt.Rows)
//{
// eventLog1.WriteEntry(dr["telephone"].ToString(), EventLogEntryType.Information, eventId++);
//}
//cs.Close();
cs.Open();
da.InsertCommand = new SqlCommand("insert into Log(IDContacts, IDEvent, Description, smsID) VALUES('5', '3', 'SMS OUT', '123')", cs); // dodelat id contacts
da.InsertCommand.ExecuteNonQuery();
cs.Close();
}
}
是否有可能如何对服务进行故障排除,或者您是否发现我的代码中有任何错误? (atm我那里只有insertCommand,后面我会在代码中添加http api)
https://imgur.com/a/O5T78E2 - 我在服务中的安装程序的 imgur 图像
添加解决方案:
所以所有问题都出在用户权限上。我使用的是本地系统服务,而不是网络服务。现在一切正常。
【问题讨论】:
-
尝试代替 Integrated Security=TRUE;" Integrated Security=SSPI;"
-
@fireshark519 它仍然什么也没做。它必须卡在 ExecuteNonQuery(); 周围的某个地方。因为如果我输入 eventLog1.WriteEntry("Test", EventLogEntryType.Information, eventId++); 它不会使这一行进入事件日志
-
SqlCommand 命令 command.ExecuteNonQuery();而不是插入命令?抱歉,我在 C# 方面不是最好的,但只是看看我写过的并且正在工作的那些 :) 我建议你也添加你得到的错误。
-
没有人,它仍然什么都不做。 @fireshark519
标签: c# sql .net wpf windows-services