简体   繁体   中英

How do i continuously update a value if it changed on the database

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");
  }

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");
   }

}

Thanks in advance!

If you can identify when the login state will change or can identify an event that's convenient to check (page load), event driven models are typically more efficient. If you don't have this luxury, then you're left polling the database every so often to see if the status has changed. Consider adding the filter to the database query rather than the client side code.

Typically, the database query is the most expensive part of the polling check. Trying to save a couple of processing cycles by checking if the new acquired matches the last check, isn't providing any sort of massive performance boost.

I am now using Devart.Data.MySql Package. this does exactly what i need.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM