Guest User

Untitled

a guest
Jun 23rd, 2018
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.01 KB | None | 0 0
  1. class Program
  2. {
  3. public class DAL
  4. {
  5. private const string _connectionString = @"Data Source=localhostfsdf;Initial Catalog=fasdfsa;Integrated Security=SSPI;";
  6.  
  7. private const string inserttStr = @"INSERT INTO dbo.testTable (test) VALUES(@test);";
  8.  
  9. /// <summary>
  10. /// Execute command on DBMS.
  11. /// </summary>
  12. /// <param name="command">Command to execute.</param>
  13. private void ExecuteNonQuery(IDbCommand command)
  14. {
  15. if (command == null)
  16. throw new ArgumentNullException("Parameter 'command' can't be null!");
  17.  
  18. using (IDbConnection connection = new SqlConnection(_connectionString))
  19. {
  20. command.Connection = connection;
  21. connection.Open();
  22. command.ExecuteNonQuery();
  23. }
  24. }
  25.  
  26. public void FirstMethod()
  27. {
  28. IDbCommand command = new SqlCommand(inserttStr);
  29. command.Parameters.Add(new SqlParameter("@test", "Hello1"));
  30.  
  31. using (TransactionScope sc = new TransactionScope(TransactionScopeOption.Required))
  32. {
  33. ExecuteNonQuery(command);
  34. sc.Complete();
  35. }
  36. }
  37.  
  38. public void SecondMethod()
  39. {
  40. IDbCommand command = new SqlCommand(inserttStr);
  41. command.Parameters.Add(new SqlParameter("@test", "Hello2"));
  42.  
  43. using (TransactionScope sc = new TransactionScope(TransactionScopeOption.Required))
  44. {
  45. ExecuteNonQuery(command);
  46. sc.Complete();
  47. }
  48. }
  49. }
  50.  
  51. static void Main(string[] args)
  52. {
  53.  
  54. DAL dal = new DAL();
  55. TransactionOptions tso = new TransactionOptions();
  56. tso.IsolationLevel = System.Transactions.IsolationLevel.ReadCommitted;
  57.  
  58. using (TransactionScope sc = new TransactionScope(TransactionScopeOption.Required,tso))
  59. {
  60. dal.FirstMethod();
  61. dal.SecondMethod();
  62. sc.Complete();
  63. }
  64. }
  65. }
Add Comment
Please, Sign In to add comment