SQL TRANSACTION .net C#
It is possible to work with more than one SQL Statement in a time. It means that if we want to insert data to database table as well as update data on database table as well as delete or so on at a same time. To handle such situation SQL Transaction will be helped to do work with more than one SQL Statement at a time. Using SQL Transaction it is very easy to work with multiple SQL Statements. When implement some SQL Transaction it check all SQL Statements execute perfectly it means that Committed or otherwise it will be rollback no SQL Statements execute.
Following below step easy to create SQL Transaction
Create SQL Connection and SQL Data Reader object
SqlConnection conn =new SqlConnection(“SQL CONNCTION STRING IS GOING HERE”);
SqlDataReader rdr = null;
Open SQL Connection
Conn.open()
Begging SQL transaction
SqlTransaction dbTrans = dbConnection.BeginTransaction();
try
{
//Set Transaction object to Sqlcommand
SqlCommand dbCmd = dbConnection.CreateCommand();
dbCmd.Transaction = dbTrans;
//SQL STATEMENT GOES HERE YOU CAN ADD ANY NUMBER OF SQL STATEMNET
//IF SQL STATEMENT EXECUTE Transction will be Commit
dbTrans.Commit();
}
catch (Exception qe)
{
//IF ANY error occure Transaction will be rollback
dbTrans.Rollback();
}
finally
{
//Close Database Connection
Conn.close();
}
Following below step easy to create SQL Transaction
Create SQL Connection and SQL Data Reader object
SqlConnection conn =new SqlConnection(“SQL CONNCTION STRING IS GOING HERE”);
SqlDataReader rdr = null;
Open SQL Connection
Conn.open()
Begging SQL transaction
SqlTransaction dbTrans = dbConnection.BeginTransaction();
try
{
//Set Transaction object to Sqlcommand
SqlCommand dbCmd = dbConnection.CreateCommand();
dbCmd.Transaction = dbTrans;
//SQL STATEMENT GOES HERE YOU CAN ADD ANY NUMBER OF SQL STATEMNET
//IF SQL STATEMENT EXECUTE Transction will be Commit
dbTrans.Commit();
}
catch (Exception qe)
{
//IF ANY error occure Transaction will be rollback
dbTrans.Rollback();
}
finally
{
//Close Database Connection
Conn.close();
}
Your Comments are highly appreciated; it will be improving my knowledge also.



0 comments:
Post a Comment