Using Trasaction with DataAdapters
Following are the steps to use SqlTransaction with SqlDataAdapter
1. Create a Connection :
SqlConnection con = new SqlConnection(connectionString);
Con.Open();
2. Create a Transaction :
SqlTransaction tran = con.BeginTrasaction();
3. Create a Command object :
SqlCommand command = new SqlCommand(query);
4. Create a DataAdapter :
SqlDataAdapter dataAdapter = new SqlDataAdapter(con);
dataAdapter.SelectCommand = command;
//Fill a dataset
//modify dataset
//Create Update, Insert, Delete command using SqlCommandBuilder and SqlDataAdapter
//update the data into Dataset to Database using update method
dataAdapter.Update(dataset);
5. Commit or Rollback the Trasaction :
Tran.Commit();
Or
Tran.Rollback();
When you create a DataAdapter using the Connection object, Trasaction associated with the connection object is also associated with the DataAdapter object.
1. Create a Connection :
SqlConnection con = new SqlConnection(connectionString);
Con.Open();
2. Create a Transaction :
SqlTransaction tran = con.BeginTrasaction();
3. Create a Command object :
SqlCommand command = new SqlCommand(query);
4. Create a DataAdapter :
SqlDataAdapter dataAdapter = new SqlDataAdapter(con);
dataAdapter.SelectCommand = command;
//Fill a dataset
//modify dataset
//Create Update, Insert, Delete command using SqlCommandBuilder and SqlDataAdapter
//update the data into Dataset to Database using update method
dataAdapter.Update(dataset);
5. Commit or Rollback the Trasaction :
Tran.Commit();
Or
Tran.Rollback();
When you create a DataAdapter using the Connection object, Trasaction associated with the connection object is also associated with the DataAdapter object.
0 Comments:
Post a Comment
<< Home