該范例演示運行來自 Command 對象和 Connection 對象的 Execute 方法。同時使用 Requery 方法檢索記錄集中的當前數(shù)據(jù),并用 Clear 方法清除 Errors 集合的內(nèi)容。運行該過程需要 ExecuteCommand 和 PrintOutput 過程。
Public Sub ExecuteX()
Dim strSQLChange As String
Dim strSQLRestore As String
Dim strCnn As String
Dim cnn1 As ADODB.Connection
Dim cmdChange As ADODB.Command
Dim rstTitles As ADODB.Recordset
Dim errLoop As ADODB.Error
' 定義兩個 SQL 語句作為命令文本執(zhí)行。
strSQLChange = "UPDATE Titles SET Type = " & _
"'self_help' WHERE Type = 'psychology'"
strSQLRestore = "UPDATE Titles SET Type = " & _
"'psychology' WHERE Type = 'self_help'"
' 打開連接。
strCnn = "Provider=sqloledb;" & _
"Data Source=srv;Initial Catalog=pubs;User Id=sa;Password=; "
Set cnn1 = New ADODB.Connection
cnn1.Open strCnn
' 創(chuàng)建命令對象。
Set cmdChange = New ADODB.Command
Set cmdChange.ActiveConnection = cnn1
cmdChange.CommandText = strSQLChange
' 打開標題表。
Set rstTitles = New ADODB.Recordset
rstTitles.Open "titles", cnn1, , , adCmdTable
' 打印原始數(shù)據(jù)報告。
Debug.Print _
"Data in Titles table before executing the query"
PrintOutput rstTitles
' 清除 Errors 集合的外部錯誤。
cnn1.Errors.Clear
' 調(diào)用 ExecuteCommand 子例程執(zhí)行 cmdChange 命令。
ExecuteCommand cmdChange, rstTitles
' 打印新數(shù)據(jù)報告。
Debug.Print _
"Data in Titles table after executing the query"
PrintOutput rstTitles
' 使用 Connection 對象的 execute 方法執(zhí)行 SQL 語句以恢復數(shù)據(jù)。
' 捕獲錯誤,必要時檢查 Errors 集合。
On Error GoTo Err_Execute
cnn1.Execute strSQLRestore, , adExecuteNoRecords
On Error GoTo 0
' 通過再查詢記錄集檢索當前數(shù)據(jù)。
rstTitles.Requery
' 打印已恢復數(shù)據(jù)的報告。
Debug.Print "Data after executing the query " & _
"to restore the original information"
PrintOutput rstTitles
rstTitles.Close
cnn1.Close
Exit Sub
Err_Execute:
' 將任何由執(zhí)行查詢引起的錯誤通知用戶。
If Errors.Count > 0 Then
For Each errLoop In Errors
MsgBox "Error number: " & errLoop.Number & vbCr & _
errLoop.Description
Next errLoop
End If
Resume Next
End Sub
Public Sub ExecuteCommand(cmdTemp As ADODB.Command, _
rstTemp As ADODB.Recordset)
Dim errLoop As Error
' 運行指定的 Command 對象。捕獲錯誤,必要時檢查 Errors 集合。
On Error GoTo Err_Execute
cmdTemp.Execute
On Error GoTo 0
' 通過再查詢記錄集檢索當前數(shù)據(jù)。
rstTemp.Requery
Exit Sub
Err_Execute:
' 將任何由執(zhí)行查詢引起的錯誤通知用戶。
If Errors.Count > 0 Then
For Each errLoop In Errors
MsgBox "Error number: " & errLoop.Number & vbCr & _
errLoop.Description
Next errLoop
End If
Resume Next
End Sub
Public Sub PrintOutput(rstTemp As ADODB.Recordset)
' 枚舉 Recordset。
Do While Not rstTemp.EOF
Debug.Print " " & rstTemp!Title & _
", " & rstTemp!Type
rstTemp.MoveNext
Loop
End Sub
VBScript 版本
下面是使用 VBScript 編寫、并用于 Active Server Page (ASP) 的相同范例。如需查看該完整功能范例,請使用與 IIS 一同安裝并位于 C:\InetPub\ASPSamp\AdvWorks 的數(shù)據(jù)源 AdvWorks.mdb,來創(chuàng)建名為 AdvWorks 的系統(tǒng)“數(shù)據(jù)源名稱”(DSN)。這是 Microsoft Access 數(shù)據(jù)庫文件。請使用查找命令定位文件 Adovbs.inc,并將其放入計劃使用的目錄中。請將以下代碼剪切并粘貼到記事本或其他文本編輯器中,另存為“Execute.asp”。這樣,便可在任何客戶端瀏覽器中查看結(jié)果。
<!-- #Include file="ADOVBS.INC" -->
<HTML><HEAD>
<TITLE>ADO Execute Method</TITLE></HEAD>
<BODY>
<FONT FACE="MS SANS SERIF" SIZE=2>
<Center><H3>ADO Execute Method</H3><H4>Recordset Retrieved Using Connection Object</H4>
<TABLE WIDTH=600 BORDER=0>
<TD VALIGN=TOP ALIGN=LEFT COLSPAN=3><FONT SIZE=2>
<!--- Recordsets 使用 Connection 和 Command 對象的 Execute 方法進行檢索 -->
<%
Set OBJdbConnection = Server.CreateObject("ADODB.Connection")
OBJdbConnection.Open "AdvWorks"
SQLQuery = "SELECT * FROM Customers"
' 第一個 Recordset RSCustomerList
Set RSCustomerList = OBJdbConnection.Execute(SQLQuery)
Set OBJdbCommand = Server.CreateObject("ADODB.Command")
OBJdbCommand.ActiveConnection = OBJdbConnection
SQLQuery2 = "SELECT * From Products"
OBJdbCommand.CommandText = SQLQuery2
Set RsProductList = OBJdbCommand.Execute
%>
<TABLE COLSPAN=8 CELLPADDING=5 BORDER=0>
<!-- Customer 表的 BEGIN 列標頭行 -->
<TR><TD ALIGN=CENTER BGCOLOR="#008080">
<FONT STYLE="ARIAL NARROW" COLOR="#ffffff" SIZE=1>Company Name</FONT>
</TD>
<TD ALIGN=CENTER BGCOLOR="#008080">
<FONT STYLE="ARIAL NARROW" COLOR="#ffffff" SIZE=1>Contact Name</FONT>
</TD>
<TD ALIGN=CENTER WIDTH=150 BGCOLOR="#008080">
<FONT STYLE="ARIAL NARROW" COLOR="#ffffff" SIZE=1>E-mail address</FONT>
</TD>
<TD ALIGN=CENTER BGCOLOR="#008080">
<FONT STYLE="ARIAL NARROW" COLOR="#ffffff" SIZE=1>City</FONT>
</TD>
<TD ALIGN=CENTER BGCOLOR="#008080">
<FONT STYLE="ARIAL NARROW" COLOR="#ffffff" SIZE=1>State/Province</FONT>
</TD></TR>
<!-- 顯示 Customer 表的 ADO 數(shù)據(jù) -->
<% Do While Not RScustomerList.EOF %>
<TR>
<TD BGCOLOR="f7efde" ALIGN=CENTER>
<FONT STYLE="ARIAL NARROW" SIZE=1>
<%= RSCustomerList("CompanyName")%>
</FONT></TD>
<TD BGCOLOR="f7efde" ALIGN=CENTER>
<FONT STYLE="ARIAL NARROW" SIZE=1>
<%= RScustomerList("ContactLastName") & ", " %>
<%= RScustomerList("ContactFirstName") %>
</FONT></TD>
<TD BGCOLOR="f7efde" ALIGN=CENTER>
<FONT STYLE="ARIAL NARROW" SIZE=1>
<%= RScustomerList("ContactLastName")%>
</FONT></TD>
<TD BGCOLOR="f7efde" ALIGN=CENTER>
<FONT STYLE="ARIAL NARROW" SIZE=1>
<%= RScustomerList("City")%>
</FONT></TD>
<TD BGCOLOR="f7efde" ALIGN=CENTER>
<FONT STYLE="ARIAL NARROW" SIZE=1>
<%= RScustomerList("StateOrProvince")%>
</FONT></TD>
</TR>
<!-Next Row = Record Loop 并添加到 html 表 -->
<%
RScustomerList.MoveNext
Loop
RScustomerList.Close
%>
</TABLE><HR>
<H4>Recordset Retrieved Using Command Object</H4>
<TABLE COLSPAN=8 CELLPADDING=5 BORDER=0>
<!-- Product List 表的 BEGIN 列標頭行 -->
<TR><TD ALIGN=CENTER BGCOLOR="#800000">
<FONT STYLE="ARIAL NARROW" COLOR="#ffffff" SIZE=1>Product Type</FONT>
</TD>
<TD ALIGN=CENTER BGCOLOR="#800000">
<FONT STYLE="ARIAL NARROW" COLOR="#ffffff" SIZE=1>Product Name</FONT>
</TD>
<TD ALIGN=CENTER WIDTH=350 BGCOLOR="#800000">
<FONT STYLE="ARIAL NARROW" COLOR="#ffffff" SIZE=1>Product Description</FONT>
</TD>
<TD ALIGN=CENTER BGCOLOR="#800000">
<FONT STYLE="ARIAL NARROW" COLOR="#ffffff" SIZE=1>Unit Price</FONT>
</TD></TR>
<!-- 顯示 ADO 數(shù)據(jù)的 Product List -->
<% Do While Not RsProductList.EOF %>
<TR>
<TD BGCOLOR="f7efde" ALIGN=CENTER>
<FONT STYLE="ARIAL NARROW" SIZE=1>
<%= RsProductList("ProductType")%>
</FONT></TD>
<TD BGCOLOR="f7efde" ALIGN=CENTER>
<FONT STYLE="ARIAL NARROW" SIZE=1>
<%= RsProductList("ProductName")%>
</FONT></TD>
<TD BGCOLOR="f7efde" ALIGN=CENTER>
<FONT STYLE="ARIAL NARROW" SIZE=1>
<%= RsProductList("ProductDescription")%>
</FONT></TD>
<TD BGCOLOR="f7efde" ALIGN=CENTER>
<FONT STYLE="ARIAL NARROW" SIZE=1>
<%= RsProductList("UnitPrice")%>
</FONT></TD>
<!-- Next Row = Record -->
<%
RsProductList.MoveNext
Loop
' 從內(nèi)存刪除對象以釋放資源。
RsProductList.Close
OBJdbConnection.Close
Set ObJdbCommand = Nothing
Set RsProductList = Nothing
Set OBJdbConnection = Nothing
%>
</TABLE></FONT></Center></BODY></HTML>