Created
June 2, 2017 17:09
-
-
Save nkundu/8b25017a14e3a9364c6c870fc10bbf75 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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