【问题标题】:How to execute SQL command in windows service?如何在 Windows 服务中执行 SQL 命令?
【发布时间】: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


【解决方案1】:

如果您对系统有足够的权限,您可以在服务运行时主动调试服务,以查看可能发生的异常。您可以参考接受的答案here,或者在您的OnStart() 回调中简单地进行以下调用:

System.Diagnostics.Debugger.Break();

当您启动服务时,您应该会收到一个提示,表明发生了未处理的异常。

点击Yes选项,对UAC提示回答Yes,选择要使用的Visual Studio实例,然后正常调试。这有望让人们更清楚地了解正在发生的事情,而不必猜测。

HTH

【讨论】:

  • 谢谢你。将来可能真的对我有帮助。
猜你喜欢
  • 2017-01-14
  • 2017-08-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-10-28
  • 2023-03-16
  • 1970-01-01
  • 2019-07-29
相关资源
最近更新 更多