开发者

Update DB column per row in per second using timer in c#

开发者 https://www.devze.com 2023-04-08 09:57 出处:网络
I have a MySql Db table.In db column there are some same type of data.I want to assign timer for different type of data.Here same type of data have same timer.For example 100,100,100 have timer1,102,1

I have a MySql Db table.In db column there are some same type of data.I want to assign timer for different type of data.Here same type of data have same timer.For example 100,100,100 have timer1, 102,102,102 have timer2 etc.And I also want to update specific 2 column and this update will be continue in per row in per second using those assigned timer.Here is my code,

 void PrepareTimers(List<int> _dataValues)
 {
     foreach (int dataValue in _dataValues)
     {
        ThreadingTimer timer = new ThreadingTimer(TimerAction, dataValue, 1000, 0);
开发者_如何学C
     }
 }

 void TimerAction(object flag)
 {
    string myconstring = "SERVER=localhost;" + "DATABASE=alicosms;" + "UID=root;" + "PASSWORD=;";
    MySqlConnection mycon = new MySqlConnection(myconstring);
    string u = "UPDATED";
    mycon.Open();
    MySqlCommand cmd = new MySqlCommand("UPDATE sms_data_bankasia set flag= @flag * 2 , sendingstatus = '" + u + "' WHERE flag = @flag", mycon);
    MySqlParameter param = new MySqlParameter();
    param.ParameterName = "@flag";
    param.Value = flag;
    cmd.Parameters.Add(param);
    cmd.ExecuteNonQuery();        
}

Its not working.What is the best way to do this?Any one can help me please?Any help can be great appriciated.


In order to update only single row at a time you need to add a LIMIT 1 clause to your sql query. It will places a limit on the number of rows that can be updated.

So your SqlCommand should be:

MySqlCommand cmd = new MySqlCommand("UPDATE sms_data_bankasia set flag= @flag * 2 , sendingstatus = '" + u + "' WHERE flag = @flag LIMIT 1", mycon);


I think I find out my problem.The problem was in timer declaration.I set period 0 and due time 1000.For this TimerAction never call again.And also Problem in query.And that the reason for not working what I want.It should be written like,

ThreadingTimer timer = new ThreadingTimer(TimerAction, dataValue, 0, 1000);
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号