Click here to Skip to main content
15,896,318 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

Is Rollback Necessary, when there is an error in Insert Query?
Posted

How about reading this one : SQL Server Transactions and Error Handling[^]
 
Share this answer
 
Comments
kgmmurugesh 1-Jul-14 2:16am    
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim sqlcmd As New SqlClient.SqlCommand

Dim con As New SqlClient.SqlConnection("Data Source=kgmmurugesh\sqlexpress;Initial Catalog=KGMERP_STORES;Integrated Security=True")

Try
con.Open()
sqlcmd.Connection = con
sqlcmd.CommandText = "begin tran ItemCreation"
sqlcmd.ExecuteNonQuery()

sqlcmd.CommandText = "Insert into Item_table(Item_Name,Item_PRate) values ('apple','a150')"
sqlcmd.ExecuteNonQuery()

sqlcmd.CommandText = "commit tran ItemCreation"
sqlcmd.ExecuteNonQuery()

Catch ex As Exception

sqlcmd.CommandText = "rollback tran ItemCreation"
sqlcmd.ExecuteNonQuery()

End Try

End Sub


This is my code, when there is a error in Insert command, it shows
The ROLLBACK TRANSACTION request has no corresponding BEGIN TRANSACTION.
The concept of rollback is an important part of the transaction concept, as well as atomicity. Please understand this:
http://en.wikipedia.org/wiki/Transaction_processing[^],
http://en.wikipedia.org/wiki/Database_transaction[^],
http://en.wikipedia.org/wiki/Atomic_transaction[^].

Do you need a rollback on error? Look at this from the atomicity point of view. If you insert updated just one record and the whole insert statement is incomplete, will your operation preserve integrity of the database? That's why you may need the rollback.

—SA
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900