MySQL 的 Visual Basic 聊天应用程序,如何在刷新时仅下载最后一条消息?

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=...")

    cn.Open()
    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()
            sb.AppendLine(r("messages"))
        End While
        TextBox2.Text = sb.ToString
    End While

此代码获取来自 MySQL table 的所有消息。我怎样才能让它只显示最后一条(以前没有读过)消息?

为代码提供某种标记以标记它停止的地方。消息的递增标识符?时间戳?这是你的数据,所以这是你的电话。但基本上,如果您要手动查看数据库中的数据,您会寻找什么值来确定 "where you left off"?这就是你想要的价值。

然后只需将该值放入 WHERE 子句即可。像这样:

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"