Leaked source code of windows server 2003
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.

77 lines
2.4 KiB

  1. <%@ LANGUAGE = PerlScript%>
  2. <html>
  3. <head>
  4. <meta name="GENERATOR" content="Tobias Martinsson">
  5. <title>ADO Transactions</title>
  6. </head>
  7. <body>
  8. <BODY BGCOLOR=#FFFFFF>
  9. <!--
  10. ActiveState PerlScript sample
  11. PerlScript: The coolest way to program custom web solutions.
  12. -->
  13. <!-- Masthead -->
  14. <TABLE CELLPADDING=3 BORDER=0 CELLSPACING=0>
  15. <TR VALIGN=TOP ><TD WIDTH=400>
  16. <A NAME="TOP"><IMG SRC="PSBWlogo.gif" WIDTH=400 HEIGHT=48 ALT="ActiveState PerlScript" BORDER=0></A><P>
  17. </TD></TR></TABLE>
  18. <HR>
  19. <H3>ActiveX Data Objects (ADO) Transactions</H3>
  20. This seemingly useless piece of code below is not that useless. It demonstrates the methods used to
  21. perform a database session in a transactional state. First, we set the Mode of the Connection object
  22. to a read/write mode, then <B>BeginTrans()</B> ignites the transaction. A query is executed and then the
  23. transaction is rolled back for fun by calling <B>RollbackTrans()</B>. Lastly, a call to <B>CommitTrans()</B> is
  24. made, and it does not do anything because the transaction has been rolled back. Something commonly misunderstood
  25. about transactions is that it will not automatically roll back if an error occurs -- you have to do error-checking
  26. and then tell the code to roll back on the occurance of an error. No changes made during the transaction are
  27. finalized or submitted until <B>CommitTrans()</B> is called, so if you remove the call to RollbackTrans(), you will
  28. see that an update occurs provided that you have the database settings configured for a database available on your system.
  29. <pre>
  30. # Create an instance of the Connection object
  31. #
  32. $conn = $Server->CreateObject('ADODB.Connection');
  33. # Open a SQL Server database
  34. #
  35. $conn->Open(<<EOF);
  36. Provider=SQLOLEDB;
  37. Persist Security Info=False;
  38. User ID=sa;
  39. Initial Catalog=MyDatabase
  40. EOF
  41. # Make sure we have read/write mode on the database unless you run the script as administrator
  42. #
  43. $conn->{Mode} = 3;
  44. # Begin the transaction here
  45. #
  46. $conn->BeginTrans();
  47. # Execute a query
  48. #
  49. $conn->Execute("INSERT into Employees VALUES ('Jdns')");
  50. # Rollback the query
  51. #
  52. $conn->RollbackTrans();
  53. # Transaction was ended with RollbackTrans, so this is not executed
  54. #
  55. $conn->CommitTrans();
  56. $conn->Close(); # Close the connection
  57. undef($conn); # Destroy the object
  58. </pre>
  59. <BR><BR>
  60. <HR>
  61. <A HREF="index.htm"> Return </A>
  62. </BODY>
  63. </HTML>