public static int ExecuteSqlTran(String connectionString, String SQLString)
{
using (SqlConnection conn = new SqlConnection(connectionString))
{
conn.Open();
SqlCommand cmd = new SqlCommand(SQLString);
cmd.Connection = conn;
SqlTransaction tx = conn.BeginTransaction();
cmd.Transaction = tx;
try
{
int count = cmd.ExecuteNonQuery();
tx.Commit();
return count;
}
catch
{
tx.Rollback();
return 0;
}
}
}
OR
using (SqlConnection connection = new SqlConnection(connectionString))
{
connection.Open();
SqlTransaction transaction = connection.BeginTransaction("SampleTransaction");
try
{
SqlHelp.ExecuteNonQuery(transaction, CommandType.Text, sql, parm);
transaction.Commit();
}
catch (Exception ex)
{
transaction.Rollback();
}
}