Advertisement
Guest User

Untitled

a guest
Apr 24th, 2019
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.47 KB | None | 0 0
  1. using System;
  2. using System.Data;
  3. using System.Data.SqlClient;
  4. using System.Data.SqlTypes;
  5. using Microsoft.SqlServer.Server;
  6. using System.Security.Principal;
  7. using System.Transactions;
  8.  
  9.  
  10. public partial class Lab8
  11. {
  12. [Microsoft.SqlServer.Server.SqlProcedure]
  13. public static void zadanie1()
  14. {
  15. WindowsIdentity newIdentity = null;
  16. WindowsImpersonationContext newContext = null;
  17. try
  18. {
  19. // zmiana tozsamosci uzytkownika
  20. newIdentity = SqlContext.WindowsIdentity;
  21. newContext = newIdentity.Impersonate();
  22. if (newContext != null)
  23. {
  24. using (SqlConnection oConn =
  25. new SqlConnection(@"Data Source=172.20.42.64;
  26. Initial Catalog=AdventureWorks2008;
  27. User ID=ALem;
  28. Password=Passw0rd;"))
  29. {
  30. SqlCommand oCmd = new SqlCommand("SELECT * FROM Person.Person",
  31. oConn);
  32. oConn.Open();
  33.  
  34. SqlDataReader oRead =
  35. oCmd.ExecuteReader(CommandBehavior.CloseConnection);
  36.  
  37. // przywracamy kontekst tozsamosci
  38. newContext.Undo();
  39.  
  40. // wyniki metoda Send
  41. SqlContext.Pipe.Send(oRead);
  42. }
  43. }
  44. else
  45. {
  46. throw new Exception("zmiana tozsamosci ");
  47. }
  48. }
  49. catch (SqlException ex)
  50. {
  51. SqlContext.Pipe.Send(ex.Message.ToString());
  52. }
  53. finally
  54. {
  55. if (newContext != null)
  56. {
  57. newContext.Undo();
  58. }
  59. }
  60. }
  61.  
  62. [Microsoft.SqlServer.Server.SqlProcedure]
  63. public static void zadanie2()
  64. {
  65. using (TransactionScope oTran = new TransactionScope())
  66. {
  67. using (SqlConnection oConn = new SqlConnection("context connection=true;"))
  68. {
  69. oConn.Open();
  70. SqlCommand oCmd =
  71. new SqlCommand("INSERT INTO AdventureWorks2008.dbo.Konta VALUES ('Adrian', 60)", oConn);
  72. oCmd.ExecuteNonQuery();
  73. oCmd.CommandText = "INSERT INTO AdventureWorks2008.dbo.Konta VALUES ('Adrian', 70)";
  74. oCmd.ExecuteNonQuery();
  75. oCmd.CommandText = "INSERT INTO AdventureWorks2008.dbo.Konta VALUES ('Ktos', 80)";
  76. oCmd.ExecuteNonQuery();
  77. oTran.Complete();
  78. }
  79. }
  80. }
  81.  
  82. [Microsoft.SqlServer.Server.SqlProcedure]
  83. public static void zadanie3(string nazwa)
  84. {
  85. SqlParameter param = new SqlParameter("@nazwa", nazwa);
  86. using(TransactionScope oTran = new TransactionScope())
  87. {
  88. using(SqlConnection oConn = new SqlConnection("context connection=true;"))
  89. {
  90. oConn.Open();
  91. SqlCommand update = new SqlCommand("UPDATE AdventureWorks2018.dbo.Konta SET value = -1 * value WHERE name = @nazwa", oConn);
  92. var returnValue = update.ExecuteNonQuery();
  93. using(SqlConnection remConn = new SqlConnection(@"Data Source=172.20.42.64;
  94. Initial Catalog=AdventureWorks2008;
  95. User ID=ALem;
  96. Password=Passw0rd;"))
  97. {
  98. returnValue = 0;
  99. remConn.Open();
  100. SqlCommand updateRemote = new SqlCommand("UPDATE AdventureWorks2018.dbo.Konta SET value = -1 * value WHERE name = @nazwa",
  101. remConn);
  102. returnValue = updateRemote.ExecuteNonQuery();
  103. }
  104. }
  105. oTran.Complete();
  106. }
  107. }
  108.  
  109. [Microsoft.SqlServer.Server.SqlProcedure]
  110. public static void zadanie4()
  111. {
  112. System.Transactions.CommittableTransaction oTran =
  113. new CommittableTransaction();
  114. using (SqlConnection oConn = new SqlConnection("context connection=true"))
  115. {
  116. try
  117. {
  118. SqlCommand oCmd = new SqlCommand();
  119. oConn.Open();
  120. //przekazujemy obiekt CommittableTransaction
  121. oConn.EnlistTransaction(oTran);
  122. oCmd.Connection = oConn;
  123. // insert nr 1
  124. oCmd.CommandText = "INSERT INTO AdventureWorks2008.dbo.Konta VALUES ('Ktos', 80)";
  125. SqlContext.Pipe.ExecuteAndSend(oCmd);
  126.  
  127. // insert nr 2
  128. oCmd.CommandText = "INSERT INTO AdventureWorks2008.dbo.Konta VALUES ('Ktos', 80)";
  129. SqlContext.Pipe.ExecuteAndSend(oCmd);
  130. // insert nr 3
  131.  
  132. oCmd.CommandText = "INSERT INTO AdventureWorks2008.dbo.Konta VALUES ('Ktos', 80)";
  133. SqlContext.Pipe.ExecuteAndSend(oCmd);
  134. }
  135. catch (SqlException ex)
  136. {
  137. oTran.Rollback();
  138. }
  139. finally
  140. {
  141. oConn.Close();
  142. }
  143. }
  144. }
  145.  
  146. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement