如何使用一个计时器从这个数据库检查数据库和另一个计时器在Windows服务中做其他工作? [英] How to use one timer to check database and another timer from this database to do other job in windows service?

查看:78
本文介绍了如何使用一个计时器从这个数据库检查数据库和另一个计时器在Windows服务中做其他工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望我的Windows服务每隔30秒用一个计时器检查SQL服务器表,并从该表中获取另一个间隔计时器值来完成另一项工作。 

如何实现这一目标?





我尝试过的事情:



 受保护 覆盖  void  OnStart( string  [] args)
{
try
{
timer1.Elapsed + = new ElapsedEventHandler(timer1_Elapsed);
timer1.Interval = 30000 ;
timer1.Enabled = true ;
timer1.Start();

}
catch (exception ex)
{
ErrorLogging(ex);
}

}

受保护 覆盖 void OnStop()
{
timer1.Enabled = false ;
}


private void timer1_Elapsed(< span class =code-keyword> object sender,System.Timers.ElapsedEventArgs e)
{
try
{

con = new SqlConnection( server = servername; database = databasename; uid = username; password = password);
con.Open();

cmd = new SqlCommand( 从[dbo]中选择TOP(1)[IntervalTime]。[Intervals] ORDER BY [IntervalTime] DESC,con);
int interval = cmd.ExecuteNonQuery();

timer2.Elapsed + = new ElapsedEventHandler(timer2_Elapsed);
timer2.Interval = interval;
timer2.Enabled = true ;
timer2.Start();
}
catch (例外情况)
{
System.Diagnostics.Debug.WriteLine(ex.Message);
}
最后
{
cmd.Dispose();
con.Dispose();
}

}


private void timer2_Elapsed( object sender,System.Timers.ElapsedEventArgs e)
{
// 做其他工作
}



任何人都可以更改此代码以使其正常工作。

解决方案

请先阅读我的评论。之后,如果您仍想使用计时器,则问题出现在

  int  interval = cmd。 ExecuteNonQuery(); 





此方法返回受查询影响的行而不是数据。你应该使用Reader然后获取专栏。



SqlCommand.ExecuteNonQuery Method(System.Data.SqlClient) [ ^ ]



c# - 如何使用executeReader()方法检索一个单元格的值 - Stack Overflow [ ^ ]


< blockquote>你的代码有一个计时器,你说你想要两个计时器。答案显而易见......



它涉及创建一个你可能称之为'timer2'的变量.....


I want my windows service to keep checking SQL server table every 30 secs with one timer and get another interval timer value from this table to do another job.

How can I achieve this?



What I have tried:

protected override void OnStart(string[] args)
        {
            try
            {
                timer1.Elapsed += new ElapsedEventHandler(timer1_Elapsed);
                timer1.Interval = 30000;
                timer1.Enabled = true;
                timer1.Start();

            }
            catch (Exception ex)
            {
                ErrorLogging(ex);
            }
            
        }

        protected override void OnStop()
        {
            timer1.Enabled = false;
        }


        private void timer1_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
        {
            try
            {

                con = new SqlConnection("server=servername; database=databasename;uid=username; password=password");
                con.Open();

                cmd = new SqlCommand("Select TOP(1) [IntervalTime] from [dbo].[Intervals] ORDER BY [IntervalTime] DESC", con);
                int interval = cmd.ExecuteNonQuery();

                timer2.Elapsed += new ElapsedEventHandler(timer2_Elapsed);
                timer2.Interval = interval;
                timer2.Enabled = true;
                timer2.Start();
            }
            catch (Exception ex)
            {
                System.Diagnostics.Debug.WriteLine(ex.Message);
            }
            finally
            {
                cmd.Dispose();
                con.Dispose();
            }

        }


        private void timer2_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
        {
// Do other job
         }


Can anyone please change this code to make it working.

解决方案

Please read my comment first. And after that if you still want to use timer the problem is in the

int interval = cmd.ExecuteNonQuery();



this method returns rows affected by the query not the data. you should use Reader and then get the column.

SqlCommand.ExecuteNonQuery Method (System.Data.SqlClient)[^]

c# - How to use executeReader() method to retrieve the value of just one cell - Stack Overflow[^]


Your code has one timer and you say you want two timers. The answer seems obvious...

It involves creating a variable you might call 'timer2'.....


这篇关于如何使用一个计时器从这个数据库检查数据库和另一个计时器在Windows服务中做其他工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆