vb.net中的SQLexception

时间:2012-09-20 17:47:25

标签: vb.net

我在SQL数据库中有两个表。一个表引用另一个表。

我在VB.NET程序的表中添加数据。我想写一个try...catch块来捕获SQLException,同时向可能发生Foreign Key违规的表中添加数据。我该怎么写呢?

2 个答案:

答案 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