Transaction in Ado.Net

Transaction is the collection of Commands. Transaction work on nothing or All function.

Means if a command fails then all command fails .

Steps to Put Transaction in Ado.net
Setp1 : create the sqlTransaction object
Step 2: Define the Connection property. And set the BeginTransaction() function with Connection object.
Step3: write the Sql Commands
Step4: pass transaction object in command()
Step5: commit()/Rollback Transaction
For Example

//create your connection property first
SqlTransaction st;
SqlCommand cmd;
con.Open();
st = con.BeginTransaction();
try
{
cmd = new SqlCommand("Insert into A (id,Fname) values (1,'aashish')", con,st);
cmd.ExecuteNonQuery();
cmd = new SqlCommand("Insert into B (id1,Lname) values (1,'aashish')", con,st);
cmd.ExecuteNonQuery();
 st.Commit();
}
catch
{
 st.Rollback();
}


Advertisements
This entry was posted in Asp.Net, Sql. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s