use SqlTransaction of ADO.NET, or TransactionScope class of .NET API.
public Boolean RunTrans(string[] sql) { Boolean b = false; SqlConnection myConnection; myConnection = new SqlConnection(ConnStr); myConnection.Open(); SqlCommand myCommand = myConnection.CreateCommand(); SqlTransaction myTrans; // Start a local transaction myTrans = myConnection.BeginTransaction("SampleTransaction"); // Must assign both transaction object and connection // to Command object for a pending local transaction myCommand.Connection = myConnection; myCommand.Transaction = myTrans; try { //myCommand.CommandText = "Insert into Region (RegionID, RegionDescription) VALUES (101, 'Description')"; //myCommand.ExecuteNonQuery(); for (int i = 0; i < Convert.ToInt32(sql.Length.ToString());i++ ) { myCommand.CommandText = sql[i]; myCommand.ExecuteNonQuery(); } myTrans.Commit(); b = true; //Console.WriteLine("Both records are written to database."); } catch (Exception e) { try { myTrans.Rollback("SampleTransaction"); } catch (SqlException ex) { if (myTrans.Connection != null) { Console.WriteLine("An exception of type " + ex.GetType() + " was encountered while attempting to roll back the transaction."); } } Console.WriteLine("An exception of type " + e.GetType() + " was encountered while inserting the data."); Console.WriteLine("Neither record was written to database."); b = false; } finally { myConnection.Close(); } return b; }
public bool ExecuteSqlTran(ArrayList SQLStringList) { bool b = false; try { Open(); OracleCommand cmd = new OracleCommand(); cmd.Connection = con; OracleTransaction tx = con.BeginTransaction(); cmd.Transaction = tx; try { for (int n = 0; n < SQLStringList.Count; n++) { string strsql = SQLStringList[n].ToString(); if (strsql.Trim().Length > 1) { cmd.CommandText = strsql; cmd.ExecuteNonQuery(); } } tx.Commit(); b = true; } catch (System.Data.OracleClient.OracleException Etx) { tx.Rollback(); //throw new Exception(Etx.Message); } } catch (System.Data.OracleClient.OracleException E) { //throw new Exception(E.Message); } finally { Close(); } return b; }
or TransactionScope class of .NET API.
{
Boolean b = false;
SqlConnection myConnection;
myConnection = new SqlConnection(ConnStr);
myConnection.Open();
SqlCommand myCommand = myConnection.CreateCommand();
SqlTransaction myTrans;
// Start a local transaction
myTrans = myConnection.BeginTransaction("SampleTransaction");
// Must assign both transaction object and connection
// to Command object for a pending local transaction
myCommand.Connection = myConnection;
myCommand.Transaction = myTrans; try
{
//myCommand.CommandText = "Insert into Region (RegionID, RegionDescription) VALUES (101, 'Description')";
//myCommand.ExecuteNonQuery();
for (int i = 0; i < Convert.ToInt32(sql.Length.ToString());i++ )
{
myCommand.CommandText = sql[i];
myCommand.ExecuteNonQuery();
}
myTrans.Commit();
b = true;
//Console.WriteLine("Both records are written to database.");
}
catch (Exception e)
{
try
{
myTrans.Rollback("SampleTransaction");
}
catch (SqlException ex)
{
if (myTrans.Connection != null)
{
Console.WriteLine("An exception of type " + ex.GetType() + " was encountered while attempting to roll back the transaction.");
}
}
Console.WriteLine("An exception of type " + e.GetType() + " was encountered while inserting the data.");
Console.WriteLine("Neither record was written to database.");
b = false;
}
finally
{
myConnection.Close();
}
return b;
}
public bool ExecuteSqlTran(ArrayList SQLStringList)
{
bool b = false;
try
{
Open();
OracleCommand cmd = new OracleCommand();
cmd.Connection = con;
OracleTransaction tx = con.BeginTransaction();
cmd.Transaction = tx;
try
{
for (int n = 0; n < SQLStringList.Count; n++)
{
string strsql = SQLStringList[n].ToString();
if (strsql.Trim().Length > 1)
{
cmd.CommandText = strsql;
cmd.ExecuteNonQuery();
}
}
tx.Commit();
b = true;
}
catch (System.Data.OracleClient.OracleException Etx)
{
tx.Rollback();
//throw new Exception(Etx.Message);
}
}
catch (System.Data.OracleClient.OracleException E)
{
//throw new Exception(E.Message);
}
finally
{
Close();
}
return b;
}