I'm working with an application which connects to a database. I definitely want to do a rollback if some database operations failed. I'm just wondering if which transaction + try-catch is better to use, the SQL or the VB.NET?
In VB.NET we can do something like:
Try
...
Catch ex As Exception
MyTransaction.Rollback
End Try
If try catch is in SQL:
...
MyCommand.CommandText = _
"USE AdventureWorks2008R2; " & _
"GO " & _
"BEGIN TRANSACTION; " & _
"BEGIN TRY " & _
" ... " & _
"END TRY " & _
"BEGIN CATCH " & _
" SELECT " & _
" ERROR_NUMBER() AS ErrorNumber " & _
" ,ERROR_SEVERITY() AS ErrorSeverity " & _
" ,ERROR_STATE() AS ErrorState " & _
" ,ERROR_PROCEDURE() AS ErrorProcedure " & _
" ,ERROR_LINE() AS ErrorLine " & _
" ,ERROR_MESSAGE() AS ErrorMessage; " & _
" IF @@TRANCOUNT > 0 " & _
" ROLLBACK TRANSACTION; " & _
"END CATCH; " & _
"IF @@TRANCOUNT > 0 " & _
" COMMIT TRANSACTION; " & _
"GO " & _
MyCommand.ExecuteNonQuery()
I always use the first one, thru VB.NET. Is there any difference? What are those? Any drawbacks?