如果值在数据库上发生更改,如何连续更新值 [英] How do I continuously update a value if it changed on the database

查看:59
本文介绍了如果值在数据库上发生更改,如何连续更新值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在WPF应用程序中实现当前登录用户的状态。我可以从数据库中读取并设置该值一次,但我需要它来继续更新。因此,例如当用户在线时,他变为忙碌。该值将在线,但一旦它在数据库上更改为忙,该值也将更新为忙。



例如:



I'm trying to implement the status of a current logged in user in an WPF application. I can read from the database and set that value once , but i need it to keep updating. So for example when a user is online, and he changes to busy. The value would be online, but once it changed on the database to busy , the value would update to busy too.

for example:

connection = new MySqlConnection(connectionString);
  Query = "SELECT status FROM Accounts WHERE username=@username";
  connection = new MySqlConnection(connectionString);

  MySqlCommand command = new MySqlCommand(Query, connection);
  command.CommandType = System.Data.CommandType.Text;
  command.Parameters.AddWithValue("@username", thedesiredusername);
  connection.Open();
  MySqlDataReader dataReader = command.ExecuteReader();

  while (dataReader.Read())
  {
     string userstatus = dataReader.GetString("status");
  }





这会将userstatus值设置一次。我怎么能让它做这样的事情:



This would set the userstatus value once. How can i get it to do something like this:

connection = new MySqlConnection(connectionString);
Query = "SELECT status FROM Accounts WHERE username=@username";
connection = new MySqlConnection(connectionString);

MySqlCommand command = new MySqlCommand(Query, connection);
command.CommandType = System.Data.CommandType.Text;
command.Parameters.AddWithValue("@username", thedesiredusername);
connection.Open();
MySqlDataReader dataReader = command.ExecuteReader();

while (dataReader.Read())
{
   //keep doing:
   if (userstatus != dataReader.GetString("status"))
   {
     string userstatus = dataReader.GetString("status");
   }

}





提前致谢!



我尝试了什么:



我已经尝试了以上内容。还尝试了TableDependency Package,但由于某种原因,我的connectionstring对此没有用。



Thanks in advance!

What I have tried:

i've tried the above . Also tried the TableDependency Package but for some reason my connectionstring didn't work on that.

推荐答案

当用户选择保持忙时,然后立即编写代码将其更新为数据库,然后在ui中忙碌覆盖在线。

在一定时间后通知用户你想要保持在线还是忙碌..
when user selects to keep busy then immediately you write code for updating it into database and then override the online with busy in ui.
give notification to user after certain time do you want to keep online or busy yet..


这篇关于如果值在数据库上发生更改,如何连续更新值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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