"If at first you don't succeed; call it version 1.0" :-Unknown

Pages

Friday, November 9, 2012

Sample (basic) Transaction handling from code side



Hi *,*,

Sample (basic) Transaction handling from code side

sqlConnection conn = db.CreateConnection();  // Create a new connection
// For Command cmd
db.AddInParameter(cmd, "@CFirstName", DbType.String, dt.Rows[i][j].ToString()); j++;
// adding new parameter Blah Blah Blah
db.AddInParameter(cmd, "@CLastName", DbType.String, dt.Rows[i][j].ToString()); j++;
// adding new parameter Blah Blah Blah
db.AddInParameter(cmd, "@CTitle", DbType.String, dt.Rows[i][j].ToString()); j++;
//adding new parameter Blah Blah Blah

// For Command cmd1
db.AddInParameter(cmd1, "@CFirstName", DbType.String, dt.Rows[i][j].ToString()); j++;
// adding new parameter Blah Blah Blah
db.AddInParameter(cmd1, "@CLastName", DbType.String, dt.Rows[i][j].ToString()); j++;
// adding new parameter Blah Blah Blah
db.AddInParameter(cmd1, "@CTitle", DbType.String, dt.Rows[i][j].ToString()); j++;
//adding new parameter Blah Blah Blah


SQLTransaction trx = conn.BeginTransaction(); // Begin Transcation

cmd.Transaction = trx; // Adding transcation to command
cmd1.Transaction = trx; // Adding transcation to command
try
{
db.ExecuteNonQuery(cmd, trx); // passing transcation to sql
db.ExecuteNonQuery(cmd1, trx); // passing transcation to sql

trx.Commit(); // Commit transcation
}
Catch(sqlexception sqlex)
{
Trx.rollback(); // Roll back Transcation
}


 


If u had any trouble just ask, Happy to help u :)
Stay Tune...
Have a nice day... 'N happy Coding :)

No comments: