Fatih Aydin Fatih Aydin - 1 year ago 90
MySQL Question

Visual Basic chat application with MySQL, how to download only last messages when refreshed?

Dim cn As New MySql.Data.MySqlClient.MySqlConnection("server=...; User ID=...; password=...; database=...")

Dim command As New MySqlCommand("select * from ...", cn)
Dim r As MySqlDataReader = command.ExecuteReader
While r.Read
Dim sb As StringBuilder = New StringBuilder()
While r.Read()
End While
TextBox2.Text = sb.ToString
End While

This code gets all messages from the MySQL table. How can i make it to only last(not read before) messages?

Answer Source

Supply to the code some kind of marker for where it left off. An incrementing identifier for the messages? A timestamp? It's your data, so it's your call. But basically, if you were to look at the data in the database manually, what value would you look for to determine "where you left off"? That's the value you want.

Then it's just a matter of putting that value into the WHERE clause. Something like this:

Dim command As New MySqlCommand("select * from SomeTable where MessageDate > @lastKnownDate", cn)
command.Parameters.Add(new MySqlParameter("@lastKnownDate", lastKnownDate))
// lastKnownDate would be the DateTime variable passed to this code.
// alternatively, use some other value as a "bookmark"
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download