VB.NET数据库检索

时间:2014-11-30 17:26:06

标签: sql database vb.net

我有一个VB.NET应用程序,它具有与数据库类似的即时消息功能。它可以很好地检索值,但问题是格式不正确。我希望格式如下:

发件人:消息

(因此...)

大卫:嘿伙计们

我在下面尝试的内容并没有让我得到我想要的结果,它只是将发件人打印在我的应用程序中富文本框的顶部,并在底部打印消息,有没有人有任何想法?

 '-------------------Retreives the message-------------------------------------------------------------
        Dim sqlStr As String = "SELECT * FROM dojodb.chats"
        Dim chatcommand As New MySqlCommand(sqlStr, MysqlConn)
        Dim rdr As MySqlDataReader = chatcommand.ExecuteReader()
        Dim tbl As New DataTable
        tbl.Load(rdr)


        '-------For every row, print the message, skip a line, and add 1 so it goes to next msg--     ------
        For i As Integer = 0 To tbl.Rows.Count - 1
            rowIndex = i

            strSender &= CStr(tbl.Rows(rowIndex)("Sender")) & vbNewLine

            strMessage &= CStr(tbl.Rows(rowIndex)("Message")) & vbNewLine

            strOutPut = strSender + ": " + strMessage

        Next
        txtGroupChat.Text = strOutPut

        'Keeps the richtextbox scrolled to the bottom so that most recent msg is always shown
        txtGroupChat.SelectionStart = txtGroupChat.Text.Length
        txtGroupChat.ScrollToCaret()

        strOutPut = "" 'clearing the string so that it does not print out duplicate info next time
        strSender = ""
        strMessage = ""
        '-------------------------End Retrive---------------------------------------

1 个答案:

答案 0 :(得分:1)

我觉得有点尴尬发布这个,但是......

  strSender = CStr(tbl.Rows(rowIndex)("Sender")) & ": "

  strMessage = CStr(tbl.Rows(rowIndex)("Message")) & vbNewLine

strOutPut &= strSender & strMessage

您认为vbNewLine有什么作用?另外,请注意& =