我正在开发一个访问数据库,我想知道如何修复下面的代码,以便我可以向用户显示正确的信息。问题是,如果oledb命令没有成功,我想显示一条错误消息。
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
答案 0 :(得分:0)
弄清楚你想要捕获哪些异常并明确捕获它们
Try
Catch se As SpecialException
MsgBox("Error1")
Catch e As Exception ' Catch all other exceptions.
MsgBox("General Error")
End Try
答案 1 :(得分:0)
这是以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
...