这是我将数据过滤到TextBoxes / Labels
的代码Dim reader As MySqlDataReader
Try
If conn.State = ConnectionState.Closed Then
conn.Open()
End If
Dim query As String
query = "SELECT * FROM member WHERE (memor = '" & memor.Text & "')"
sqlcom = New MySqlCommand(query, conn)
reader = sqlcom.ExecuteReader
While reader.Read()
Me.lblname.Text = reader("membname").ToString
Me.txtmembtype.Text = reader("membtype").ToString
Me.lblmembdate.Text = reader("membdate").ToString
End While
conn.Close()
Catch ex As Exception
MessageBox.Show(ex.Message)
Finally
conn.Dispose()
End Try
现在我想要一个消息框,提示我的mysql数据库中没有找到数据?有什么想法吗?
答案 0 :(得分:0)
您只需使用reader.HasRows()
检查记录
reader = sqlcom.ExecuteReader
if Reader.HasRows() Then
While reader.Read()
Me.lblname.Text = reader("membname").ToString
Me.txtmembtype.Text = reader("membtype").ToString
Me.lblmembdate.Text = reader("membdate").ToString
End While
Else
MessageBox.Show("No records found")
End If
答案 1 :(得分:0)
殴打,但仍然发布强制性SQL parameterization警告。
修订代码:
Try
If conn.State = ConnectionState.Closed Then
conn.Open()
End If
Dim query As String
query = "SELECT * FROM member WHERE (memor = @memor)"
sqlcom = New MySqlCommand(query, conn)
sqlcom.Parameters.AddWithValue("@memor", memor.Text)
reader = sqlcom.ExecuteReader
If reader.HasRows Then
While reader.Read()
Me.lblname.Text = reader("membname").ToString
Me.txtmembtype.Text = reader("membtype").ToString
Me.lblmembdate.Text = reader("membdate").ToString
End While
Else
MsgBox("No data found.")
End If
conn.Close()
Catch ex As Exception
MessageBox.Show(ex.Message)
Finally
conn.Dispose()
End Try