Created
May 6, 2015 21:00
-
-
Save ashtewari/15a0a56256604f6b2f43 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
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