Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ashtewari/b0da28ad3efd787a7e61 to your computer and use it in GitHub Desktop.
Save ashtewari/b0da28ad3efd787a7e61 to your computer and use it in GitHub Desktop.
private static void ExecuteSqlTransactionUsingTransactionScope(string connectionString)
{
using(TransactionScope scope = new TransactionScope())
{
using (SqlConnection connection = new SqlConnection(connectionString))
{
connection.Open();
SqlCommand command = connection.CreateCommand();
command.Connection = connection;
command.CommandText = "Insert into MyTable(ColumnA, ColumnB) VALUES (1234, 'ValueB')";
command.ExecuteNonQuery();
command.CommandText = "Insert into MyTable(ColumnA, ColumnB) VALUES (5678, 'Another Value')";
command.ExecuteNonQuery();
Console.WriteLine("Both records are written to database.");
}
scope.Complete();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment