Skip to content

Instantly share code, notes, and snippets.

@nkundu
Created June 2, 2017 17:09
Show Gist options
  • Save nkundu/8b25017a14e3a9364c6c870fc10bbf75 to your computer and use it in GitHub Desktop.
Save nkundu/8b25017a14e3a9364c6c870fc10bbf75 to your computer and use it in GitHub Desktop.
using (var conn = new System.Data.SqlClient.SqlConnection(connStr))
{
if (conn.State != System.Data.ConnectionState.Open) { conn.Open(); }
var tran = conn.BeginTransaction();
try
{
using (var cmd = conn.CreateCommand())
{
cmd.Transaction = tran;
cmd.CommandText = sql;
cmd.ExecuteNonQuery();
}
// ...
tran.Commit();
}
catch (Exception ex)
{
tran.Rollback();
throw;
}
finally
{
tran.Dispose();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment