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/15a0a56256604f6b2f43 to your computer and use it in GitHub Desktop.
Save ashtewari/15a0a56256604f6b2f43 to your computer and use it in GitHub Desktop.
private static void ExecuteTwoMethodsOpeningTwoConnections(string connectionString)
{
using(TransactionScope scope = new TransactionScope())
{
ExecuteCommandA(connectionString);
ExecuteCommandB(connectionString);
Console.WriteLine("Both records are written to database transactionally.");
scope.Complete();
}
}
private static void ExecuteCommandA(string connectionString)
{
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();
}
}
private static void ExecuteCommandB(string connectionString)
{
using (SqlConnection connection = new SqlConnection(connectionString))
{
connection.Open();
SqlCommand command = connection.CreateCommand();
command.Connection = connection;
command.CommandText = "Insert into MyTable(ColumnA, ColumnB) VALUES (5678, 'Another Value')";
command.ExecuteNonQuery();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment