如果没有找到记录,如何显示消息

时间:2015-10-20 15:05:09

标签: mysql vb.net

这是我将数据过滤到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数据库中没有找到数据?有什么想法吗?

2 个答案:

答案 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