Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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