If we are doing two or more tasks with database, then we should have to use transaction to ensure the task had been completed successfully.
There are two methods to do this task.
- Using Store procedure in database
- Using C# code.
Syntax for transaction in store procedure ,please refer by old post
Using C# code
Ado.net 2.0 and later vesion support transaction feature.
We can write code like this.
using (SqlConnection connection =
using (SqlCommand command =
SqlTransaction transaction = null;
// BeginTransaction() Requires Open Connection
transaction = connection.BeginTransaction();
// Assign Transaction to Command
command.Transaction = transaction;
// Execute 1st Command
command.CommandText = “Insert …”;
// Execute 2nd Command
command.CommandText = “Update…”;