Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Program
- {
- public class DAL
- {
- private const string _connectionString = @"Data Source=localhostfsdf;Initial Catalog=fasdfsa;Integrated Security=SSPI;";
- private const string inserttStr = @"INSERT INTO dbo.testTable (test) VALUES(@test);";
- /// <summary>
- /// Execute command on DBMS.
- /// </summary>
- /// <param name="command">Command to execute.</param>
- private void ExecuteNonQuery(IDbCommand command)
- {
- if (command == null)
- throw new ArgumentNullException("Parameter 'command' can't be null!");
- using (IDbConnection connection = new SqlConnection(_connectionString))
- {
- command.Connection = connection;
- connection.Open();
- command.ExecuteNonQuery();
- }
- }
- public void FirstMethod()
- {
- IDbCommand command = new SqlCommand(inserttStr);
- command.Parameters.Add(new SqlParameter("@test", "Hello1"));
- using (TransactionScope sc = new TransactionScope(TransactionScopeOption.Required))
- {
- ExecuteNonQuery(command);
- sc.Complete();
- }
- }
- public void SecondMethod()
- {
- IDbCommand command = new SqlCommand(inserttStr);
- command.Parameters.Add(new SqlParameter("@test", "Hello2"));
- using (TransactionScope sc = new TransactionScope(TransactionScopeOption.Required))
- {
- ExecuteNonQuery(command);
- sc.Complete();
- }
- }
- }
- static void Main(string[] args)
- {
- DAL dal = new DAL();
- TransactionOptions tso = new TransactionOptions();
- tso.IsolationLevel = System.Transactions.IsolationLevel.ReadCommitted;
- using (TransactionScope sc = new TransactionScope(TransactionScopeOption.Required,tso))
- {
- dal.FirstMethod();
- dal.SecondMethod();
- sc.Complete();
- }
- }
- }
Add Comment
Please, Sign In to add comment