2014-05-08 111 views
0

我有這段代碼,我寫了一個數據庫中是否存在記錄。當找到記錄時它運作良好。如果不是,則會引發錯誤。我想這個錯誤會被記錄在一個消息框中,表示「找不到記錄」。如何捕獲oledbdatareader錯誤

  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() 
+0

之前,你'.Read'檢查'numeri.HasRows' – Plutonix

回答

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