[英]Refreshing Database VB.NET
因此,我具有用于数据库的即时消息传递功能。 每次发送消息时,它都会将数据库中消息列中的内容打印到我的vb.net应用程序的富文本框中
我的问题是。 我必须单击两次“发送消息”按钮才能使用该功能,因为我第一次单击该按钮,没有任何反应
有人知道我哪里出错了吗? 非常感激!
Try
'----------------Sends the message-------------------------------------
MysqlConn.Open() ' opening the connection to the DB
Dim query As String
query = "insert into dojodb.chats (Message) values ('" & txtMessage.Text & "')"
command = New MySqlCommand(query, MysqlConn)
reader = command.ExecuteReader 'executes the command and reads data from db
reader.Close()
'-------------------Retreives the message------------------------------------
Dim sqlStr As String = "SELECT * FROM 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
strOutPut &= CStr(tbl.Rows(rowIndex)("Message")) & vbNewLine
i = i + 1
Next
txtGroupChat.Text = strOutPut
strOutPut = "" 'clearing the string so that it does not print out duplicate info next time
'-------------------------End Retrieve-------------------------------------------
MysqlConn.Close()
Catch ex As Exception
MessageBox.Show(ex.Message) 'printing the exact error to help future testing if needed
Finally
MysqlConn.Dispose()
End Try
End Sub
我认为您的问题是此部分:
'-------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
strOutPut &= CStr(tbl.Rows(rowIndex)("Message")) & vbNewLine
i = i + 1
Next
你为什么跳过一行? 这将导致表中的所有其他消息都不会被写出,因此这就是为什么您必须按两次以使其显示出来的原因。 您无需在For
循环中手动增加索引器,建议您尝试以下操作:
For i As Integer = 0 To tbl.Rows.Count - 1
rowIndex = i
strOutPut &= CStr(tbl.Rows(rowIndex)("Message")) & vbNewLine
Next
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.