Ruben Versavel
Ruben Versavel

Reputation: 161

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!

Upvotes: 0

Views: 502

Answers (2)

Ruben Versavel
Ruben Versavel

Reputation: 161

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

Upvotes: 0

user3112728
user3112728

Reputation: 405

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.

Upvotes: 1

Related Questions