The next generation of the Teknik Services. Written in ASP.NET. https://www.teknik.io/
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

MySqlTransaction.xml 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329
  1. <docs>
  2. <Class>
  3. <summary>
  4. Represents a SQL transaction to be made in a MySQL database. This class cannot be inherited.
  5. </summary>
  6. <remarks>
  7. The application creates a <B>MySqlTransaction</B> object by calling <see cref="MySqlConnection.BeginTransaction()"/>
  8. on the <see cref="MySqlConnection"/> object. All subsequent operations associated with the
  9. transaction (for example, committing or aborting the transaction), are performed on the
  10. <B>MySqlTransaction</B> object.
  11. </remarks>
  12. <example>
  13. The following example creates a <see cref="MySqlConnection"/> and a <B>MySqlTransaction</B>.
  14. It also demonstrates how to use the <see cref="MySqlConnection.BeginTransaction()"/>,
  15. <see cref="MySqlTransaction.Commit"/>, and <see cref="MySqlTransaction.Rollback"/> methods.
  16. <code lang="vbnet">
  17. Public Sub RunTransaction(myConnString As String)
  18. Dim myConnection As New MySqlConnection(myConnString)
  19. myConnection.Open()
  20. Dim myCommand As MySqlCommand = myConnection.CreateCommand()
  21. Dim myTrans As MySqlTransaction
  22. ' Start a local transaction
  23. myTrans = myConnection.BeginTransaction()
  24. ' Must assign both transaction object and connection
  25. ' to Command object for a pending local transaction
  26. myCommand.Connection = myConnection
  27. myCommand.Transaction = myTrans
  28. Try
  29. myCommand.CommandText = "Insert into Region (RegionID, RegionDescription) VALUES (100, 'Description')"
  30. myCommand.ExecuteNonQuery()
  31. myCommand.CommandText = "Insert into Region (RegionID, RegionDescription) VALUES (101, 'Description')"
  32. myCommand.ExecuteNonQuery()
  33. myTrans.Commit()
  34. Console.WriteLine("Both records are written to database.")
  35. Catch e As Exception
  36. Try
  37. myTrans.Rollback()
  38. Catch ex As MySqlException
  39. If Not myTrans.Connection Is Nothing Then
  40. Console.WriteLine("An exception of type " &amp; ex.GetType().ToString() &amp; _
  41. " was encountered while attempting to roll back the transaction.")
  42. End If
  43. End Try
  44. Console.WriteLine("An exception of type " &amp; e.GetType().ToString() &amp; _
  45. "was encountered while inserting the data.")
  46. Console.WriteLine("Neither record was written to database.")
  47. Finally
  48. myConnection.Close()
  49. End Try
  50. End Sub 'RunTransaction
  51. </code>
  52. <code lang="C#">
  53. public void RunTransaction(string myConnString)
  54. {
  55. MySqlConnection myConnection = new MySqlConnection(myConnString);
  56. myConnection.Open();
  57. MySqlCommand myCommand = myConnection.CreateCommand();
  58. MySqlTransaction myTrans;
  59. // Start a local transaction
  60. myTrans = myConnection.BeginTransaction();
  61. // Must assign both transaction object and connection
  62. // to Command object for a pending local transaction
  63. myCommand.Connection = myConnection;
  64. myCommand.Transaction = myTrans;
  65. try
  66. {
  67. myCommand.CommandText = "Insert into Region (RegionID, RegionDescription) VALUES (100, 'Description')";
  68. myCommand.ExecuteNonQuery();
  69. myCommand.CommandText = "Insert into Region (RegionID, RegionDescription) VALUES (101, 'Description')";
  70. myCommand.ExecuteNonQuery();
  71. myTrans.Commit();
  72. Console.WriteLine("Both records are written to database.");
  73. }
  74. catch(Exception e)
  75. {
  76. try
  77. {
  78. myTrans.Rollback();
  79. }
  80. catch (MySqlException ex)
  81. {
  82. if (myTrans.Connection != null)
  83. {
  84. Console.WriteLine("An exception of type " + ex.GetType() +
  85. " was encountered while attempting to roll back the transaction.");
  86. }
  87. }
  88. Console.WriteLine("An exception of type " + e.GetType() +
  89. " was encountered while inserting the data.");
  90. Console.WriteLine("Neither record was written to database.");
  91. }
  92. finally
  93. {
  94. myConnection.Close();
  95. }
  96. }
  97. </code>
  98. </example>
  99. </Class>
  100. <Rollback>
  101. <summary>
  102. Rolls back a transaction from a pending state.
  103. </summary>
  104. <remarks>
  105. The Rollback method is equivalent to the MySQL statement ROLLBACK.
  106. The transaction can only be rolled back from a pending state
  107. (after BeginTransaction has been called, but before Commit is
  108. called).
  109. </remarks>
  110. <example>
  111. The following example creates a <see cref="MySqlConnection"/> and a
  112. <see cref="MySqlTransaction"/>. It also demonstrates how to use the
  113. <see cref="MySqlConnection.BeginTransaction()"/>, <see cref="Commit"/>, and <B>Rollback</B>
  114. methods.
  115. <code lang="vbnet">
  116. Public Sub RunSqlTransaction(myConnString As String)
  117. Dim myConnection As New MySqlConnection(myConnString)
  118. myConnection.Open()
  119. Dim myCommand As MySqlCommand = myConnection.CreateCommand()
  120. Dim myTrans As MySqlTransaction
  121. ' Start a local transaction
  122. myTrans = myConnection.BeginTransaction()
  123. ' Must assign both transaction object and connection
  124. ' to Command object for a pending local transaction
  125. myCommand.Connection = myConnection
  126. myCommand.Transaction = myTrans
  127. Try
  128. myCommand.CommandText = "Insert into mytable (id, desc) VALUES (100, 'Description')"
  129. myCommand.ExecuteNonQuery()
  130. myCommand.CommandText = "Insert into mytable (id, desc) VALUES (101, 'Description')"
  131. myCommand.ExecuteNonQuery()
  132. myTrans.Commit()
  133. Console.WriteLine("Success.")
  134. Catch e As Exception
  135. Try
  136. myTrans.Rollback()
  137. Catch ex As MySqlException
  138. If Not myTrans.Connection Is Nothing Then
  139. Console.WriteLine("An exception of type " &amp; ex.GetType().ToString() &amp; _
  140. " was encountered while attempting to roll back the transaction.")
  141. End If
  142. End Try
  143. Console.WriteLine("An exception of type " &amp; e.GetType().ToString() &amp; _
  144. "was encountered while inserting the data.")
  145. Console.WriteLine("Neither record was written to database.")
  146. Finally
  147. myConnection.Close()
  148. End Try
  149. End Sub
  150. </code>
  151. <code lang="C#">
  152. public void RunSqlTransaction(string myConnString)
  153. {
  154. MySqlConnection myConnection = new MySqlConnection(myConnString);
  155. myConnection.Open();
  156. MySqlCommand myCommand = myConnection.CreateCommand();
  157. MySqlTransaction myTrans;
  158. // Start a local transaction
  159. myTrans = myConnection.BeginTransaction();
  160. // Must assign both transaction object and connection
  161. // to Command object for a pending local transaction
  162. myCommand.Connection = myConnection;
  163. myCommand.Transaction = myTrans;
  164. try
  165. {
  166. myCommand.CommandText = "Insert into mytable (id, desc) VALUES (100, 'Description')";
  167. myCommand.ExecuteNonQuery();
  168. myCommand.CommandText = "Insert into mytable (id, desc) VALUES (101, 'Description')";
  169. myCommand.ExecuteNonQuery();
  170. myTrans.Commit();
  171. Console.WriteLine("Both records are written to database.");
  172. }
  173. catch(Exception e)
  174. {
  175. try
  176. {
  177. myTrans.Rollback();
  178. }
  179. catch (MySqlException ex)
  180. {
  181. if (myTrans.Connection != null)
  182. {
  183. Console.WriteLine("An exception of type " + ex.GetType() +
  184. " was encountered while attempting to roll back the transaction.");
  185. }
  186. }
  187. Console.WriteLine("An exception of type " + e.GetType() +
  188. " was encountered while inserting the data.");
  189. Console.WriteLine("Neither record was written to database.");
  190. }
  191. finally
  192. {
  193. myConnection.Close();
  194. }
  195. }
  196. </code>
  197. </example>
  198. </Rollback>
  199. <Commit>
  200. <summary>
  201. Commits the database transaction.
  202. </summary>
  203. <remarks>
  204. The <b>Commit</b> method is equivalent to the MySQL SQL statement
  205. COMMIT.
  206. </remarks>
  207. <example>
  208. The following example creates a <see cref="MySqlConnection"/> and a
  209. <see cref="MySqlTransaction"/>. It also demonstrates how to use the
  210. <see cref="MySqlConnection.BeginTransaction()"/>, <see cref="Commit"/>, and <B>Rollback</B>
  211. methods.
  212. <code lang="vbnet">
  213. Public Sub RunSqlTransaction(myConnString As String)
  214. Dim myConnection As New MySqlConnection(myConnString)
  215. myConnection.Open()
  216. Dim myCommand As MySqlCommand = myConnection.CreateCommand()
  217. Dim myTrans As MySqlTransaction
  218. ' Start a local transaction
  219. myTrans = myConnection.BeginTransaction()
  220. ' Must assign both transaction object and connection
  221. ' to Command object for a pending local transaction
  222. myCommand.Connection = myConnection
  223. myCommand.Transaction = myTrans
  224. Try
  225. myCommand.CommandText = "Insert into mytable (id, desc) VALUES (100, 'Description')"
  226. myCommand.ExecuteNonQuery()
  227. myCommand.CommandText = "Insert into mytable (id, desc) VALUES (101, 'Description')"
  228. myCommand.ExecuteNonQuery()
  229. myTrans.Commit()
  230. Console.WriteLine("Success.")
  231. Catch e As Exception
  232. Try
  233. myTrans.Rollback()
  234. Catch ex As MySqlException
  235. If Not myTrans.Connection Is Nothing Then
  236. Console.WriteLine("An exception of type " &amp; ex.GetType().ToString() &amp; _
  237. " was encountered while attempting to roll back the transaction.")
  238. End If
  239. End Try
  240. Console.WriteLine("An exception of type " &amp; e.GetType().ToString() &amp; _
  241. "was encountered while inserting the data.")
  242. Console.WriteLine("Neither record was written to database.")
  243. Finally
  244. myConnection.Close()
  245. End Try
  246. End Sub
  247. </code>
  248. <code lang="C#">
  249. public void RunSqlTransaction(string myConnString)
  250. {
  251. MySqlConnection myConnection = new MySqlConnection(myConnString);
  252. myConnection.Open();
  253. MySqlCommand myCommand = myConnection.CreateCommand();
  254. MySqlTransaction myTrans;
  255. // Start a local transaction
  256. myTrans = myConnection.BeginTransaction();
  257. // Must assign both transaction object and connection
  258. // to Command object for a pending local transaction
  259. myCommand.Connection = myConnection;
  260. myCommand.Transaction = myTrans;
  261. try
  262. {
  263. myCommand.CommandText = "Insert into mytable (id, desc) VALUES (100, 'Description')";
  264. myCommand.ExecuteNonQuery();
  265. myCommand.CommandText = "Insert into mytable (id, desc) VALUES (101, 'Description')";
  266. myCommand.ExecuteNonQuery();
  267. myTrans.Commit();
  268. Console.WriteLine("Both records are written to database.");
  269. }
  270. catch(Exception e)
  271. {
  272. try
  273. {
  274. myTrans.Rollback();
  275. }
  276. catch (MySqlException ex)
  277. {
  278. if (myTrans.Connection != null)
  279. {
  280. Console.WriteLine("An exception of type " + ex.GetType() +
  281. " was encountered while attempting to roll back the transaction.");
  282. }
  283. }
  284. Console.WriteLine("An exception of type " + e.GetType() +
  285. " was encountered while inserting the data.");
  286. Console.WriteLine("Neither record was written to database.");
  287. }
  288. finally
  289. {
  290. myConnection.Close();
  291. }
  292. }
  293. </code>
  294. </example>
  295. </Commit>
  296. </docs>