ADO Transaction Control

Source: Internet
Author: User

  1. In ADO, you can use connection and transaction objects to control transactions. To perform a transaction, follow these steps:
  2. 1. Call the BeginTransaction method of the Connection object to mark the beginning of the transaction.
  3. 2. Assign the transaction object to the transaction property of the command to be executed.
  4. 3. Execute the required command.
  5. 4. Call the Commit method of the transaction object to complete the transaction, or call the Rollback method to cancel the transaction. Of course, there are advantages and disadvantages to the processing of ADO, and we use them to see the situation.
  6. Pros: – Simplicity – almost as fast as data transactions – database-independent, proprietary code for different databases is hidden
  7. Cons: – A transaction cannot span multiple database connections – The transaction executes on the database connection layer, so a database connection needs to be maintained during the transaction.
  8. Below we look at an example, create a page, the same simple, just need a button, and then programming:
  9. 1 using System;
  10. 2 using System.Data;
  11. 3 using System.Configuration;
  12. 4 using System.Collections;
  13. 5 using System.Web;
  14. 6 using System.Web.Security;
  15. 7 using System.Web.UI;
  16. 8 using System.Web.UI.WebControls;
  17. 9 using System.Web.UI.WebControls.WebParts;
  18. Ten using System.Web.UI.HtmlControls;
  19. One using System.Data.SqlClient;
  20. 12
  21. namespace WebApplication1
  22. 14 {
  23. Public partial class AdoAction:System.Web.UI.Page
  24. 16 {
  25. protected void Page_Load ( object sender, EventArgs e)
  26. 18 {
  27. 19
  28. 20}
  29. 21st
  30. protected void Btn_click ( object sender, EventArgs e)
  31. 23 {
  32. SqlConnection con = new SqlConnection ();
  33. -Con. ConnectionString = configurationmanager.connectionstrings[ "DSN"].   ConnectionString;
  34. -Con. Open ();
  35. //Start a transaction.
  36. SqlTransaction Mytran = con. BeginTransaction ();
  37. //Create a command for a transaction, note that we execute a double command, and the first execution is of course successful.   We do it again, fail.
  38. The third time we change one of the orders, the other does not change, this time the transaction will error, this is the transaction mechanism.
  39. SqlCommand mycom = new SqlCommand ();
  40. Mycom.connection = con;
  41. Mycom.transaction = Mytran;
  42. Try
  43. 35 {
  44. Mycom.commandtext = "INSERT into sqlaction values (' Test 2 ', ' 111 ')";
  45. Panax Notoginseng mycom.executenonquery ();
  46. Mycom.commandtext = "INSERT into sqlaction values (' Test 3 ', ' 111 ')";
  47. Mycom.executenonquery ();
  48. Mytran.commit ();
  49. Response.Write ( "Successful Execution");
  50. 42
  51. 43}
  52. (Exception Ex)
  53. 45 {
  54. Mytran.rollback ();
  55. I //Create and return an exception error message
  56. Response.Write (Ex.tostring ());
  57. Response.Write ( "write database Failed");
  58. 50}
  59. Wuyi finally
  60. 52 {
  61. The con. Close ();
  62. 54}

ADO Transaction Control

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.