如何捕获oledbdatareader错误

时间:2014-05-08 23:25:01

标签: vb.net

我写了这个代码,以查明数据库中是否存在记录。找到记录时效果很好。如果不是,则会出现错误。我希望错误被捕获在一个消息框中,该消息框指出"记录未找到"代替。

         Dim findprinc As String = TextBox1.Text.Substring(0, 16)
        MsgBox(findprinc)
        sql = "Select RealID from Dets where ID like '%" & findprinc &                "%'"
        MsgBox(sql)
        Dim conn As New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0; Persist Security Info=false; Data Source=..\new.mdb")
        conn.Open()
        Dim cmd As New OleDbCommand(sql, conn)
        Dim numeri As OleDbDataReader = cmd.ExecuteReader
        numeri.Read()

1 个答案:

答案 0 :(得分:0)

 Dim findprinc As String = TextBox1.Text.Substring(0, 16)
    MsgBox(findprinc)
    Sql = "Select RealID from Dets where ID like '%" & findprinc & "%'"
    MsgBox(Sql)
    Dim conn As New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0; Persist Security Info=false; Data Source=..\new.mdb")
    conn.Open()
    Dim cmd As New OleDbCommand(Sql, conn)
    Dim numeri As OleDbDataReader = cmd.ExecuteReader
    Dim recordFound As Boolean = False
    While numeri.Read
        recordFound = True
    End While
    If recordFound = False Then
        MsgBox("Record Not Found")
    End If