2012-09-20 34 views
0

我在SQL數據庫中有兩個表。一個表正在引用另一個表。vb.net中的SQLexception

我正在從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

+0

問題是關於VB.Net,並且您編寫的代碼甚至不會編譯(在VB.Net和C#中)。這是不好的做法,寫'拋出前',應該只是'扔' – jeroenh

+0

對不起,我更新 –

+0

更好的是:-) – jeroenh

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