如何检查oledbcommand是否成功?

问题描述:

我正在访问数据库,我想知道如何修复下面的代码,以便我可以向用户显示正确的信息。问题是,如果oledb命令没有成功,我想显示一条错误消息。如何检查oledbcommand是否成功?

 Try 

     cn = New OleDbConnection(" Provider=Microsoft.Jet.OLEDB.4.0;Data Source=F:\ro.mdb") 
     'provider to be used when working with access database 
     cn.Open() 
     cmd = New OleDbCommand("select * from [Table1]", cn) 

     cmd.CommandText = "insert into [Table1]([LNAME], [FNAME], [MNAME], [POS], [UNAME], [PASS]) values('" + lstname + "','" + frsname + "','" + midname + "','" + post + "','" + usrname + "','" + pword + "')" 


     cmd.ExecuteNonQuery() 

    Catch 
     MsgBox("Either one of the text box is empty or the IDNUMBER entered is already on the database", MsgBoxStyle.Critical) 


    End Try 

这开始作为一个SQLEXCEPTION并改为OLEDB这个问题。对不起,没有测试。

Try  
     ... 
    Catch dbException as OledbException 
     Dim myErrors as OledbErrorCollection = dbException.Errors 
     Dim i as Integer 
      For i = 0 to myErrors.Count - 1 
       MessageBox.Show("Index #" & i & ControlChars.Cr & _ 
        "Error: " & myErrors(i).ToString() & ControlChars.Cr) 
      Next i 
    Finially 
     ... 

弄清楚你想要什么例外陷阱,并抓住他们明确


Try 

Catch se As SpecialException 
    MsgBox("Error1") 

Catch e As Exception ' Catch all other exceptions. 
    MsgBox("General Error") 

End Try