我在SQL数据库中有两个表。一个表引用另一个表。
我在VB.NET程序的表中添加数据。我想写一个try...catch
块来捕获SQLException
,同时向可能发生Foreign Key
违规的表中添加数据。我该怎么写呢?
答案 0 :(得分:1)
您可以尝试使用此代码
Try
...
Catch ex As Exception
MsgBox("Your exception" & ex.Message)
End Try
链接:http://msdn.microsoft.com/fr-fr/library/0yd65esw%28v=vs.80%29.aspx
答案 1 :(得分:0)
Try
'Do your stuff
Catch ex as SqlException
Dim errors = ex.Errors
' inspect errors to decide if this is the condition you want to catch
Dim shouldCatch As Boolean = ...
' Code that acts on the specific error condition
If shouldCatch Then
Else
Throw ' rethrow everything we're not interested in
End If
End Try