home *** CD-ROM | disk | FTP | other *** search
/ PC World 2003 March / PCWorld_2003-03_cd.bin / Software / Topware / activeperl / ActivePerl / Perl / eg / aspSamples / ado14.asp < prev    next >
Encoding:
Text File  |  2000-08-10  |  2.4 KB  |  78 lines

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