ASP Source Code:
<!-- #include file="adovbs.inc" -->
<%
' ADO constants included above. Questions about adovbs.inc?
' See "What is Adovbs.inc and Why Do I Need It?"
' http://www.asp101.com/articles/john/adovbs/default.asp
Dim strConnString ' Connection string
Dim myConnection ' ADO Connection object
Dim strSqlQuery ' SQL query
Dim lngRecordsAffected ' Number of records affected by command
' Set our connection string
strConnString = "Provider=SQLOLEDB;Data Source=10.2.2.133;" _
& "Initial Catalog=samples;User Id=samples;Password=password;" _
& "Connect Timeout=15;Network Library=dbmssocn;"
' Build our SQL query
strSqlQuery = "INSERT INTO [scratch] (text_field, integer_field, date_time_field) " _
& "VALUES ('" _
& CStr(WeekdayName(WeekDay(Date()))) & "', '" _
& CInt(Day(Now())) & "', '" _
& Now() & "');"
' Open connection
Set myConnection = Server.CreateObject("ADODB.Connection")
myConnection.Open strConnString
' Start transaction
Response.Write "<p><strong>Starting Transaction.</strong></p>" & vbCrLf
myConnection.BeginTrans
' Execute the SQL command
Response.Write "<p><strong>Executing SQL Command:</strong><br /><code>" & strSqlQuery & "</code></p>" & vbCrLf
myConnection.Execute strSqlQuery, lngRecordsAffected, adCmdText + adExecuteNoRecords
' Echo back the number of records affected
Response.Write "<p><strong>Records Affected:</strong> <code>" & lngRecordsAffected & "</code></p>" & vbCrLf
' Either commit or rollback the transaction based on QueryString
If CBool(Request.QueryString("rollback")) = True Then
Response.Write "<p><strong>Rolling Back Transaction.</strong></p>" & vbCrLf
myConnection.RollbackTrans
Else
Response.Write "<p><strong>Commiting Transaction.</strong></p>" & vbCrLf
myConnection.CommitTrans
End If
' Close data access objects and free variables
myConnection.Close
Set myConnection = Nothing
%>
<br />
<p>
<strong>Run the script again:</strong>
<a href="<%= Request.ServerVariables("URL") %>?rollback=false">Commit Transaction</a>
or
<a href="<%= Request.ServerVariables("URL") %>?rollback=true">Rollback Transaction</a>
</p>
|