Execute、Requery 與 Clear 方法範例

此範例示範從 Command 物件與 Connection 物件兩者執行階段的 Execute 方法。也使用 Requery 方法擷取 recordset 中目前的資料,並使用 Clear 方法清除 Errors 集合物件的內容。執行此程序需要有 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

    ' Define two SQL statements to execute as command text.
    strSQLChange = "UPDATE Titles SET Type = " & _
        "'self_help' WHERE Type = 'psychology'"
    strSQLRestore = "UPDATE Titles SET Type = " & _
        "'psychology' WHERE Type = 'self_help'"

    ' Open connection.
        strCnn = "Provider=sqloledb;" & _
        "Data Source=srv;Initial Catalog=pubs;User Id=sa;Password=; "
    Set cnn1 = New ADODB.Connection
    cnn1.Open strCnn

    ' Create command object.
    Set cmdChange = New ADODB.Command
    Set cmdChange.ActiveConnection = cnn1
    cmdChange.CommandText = strSQLChange
    
    ' Open titles table.
    Set rstTitles = New ADODB.Recordset
    rstTitles.Open "titles", cnn1, , , adCmdTable

    ' Print report of original data.
    Debug.Print _
        "Data in Titles table before executing the query"
    PrintOutput rstTitles

    ' Clear extraneous errors from the Errors collection.
    cnn1.Errors.Clear

    ' Call the ExecuteCommand subroutine to execute cmdChange command.
    ExecuteCommand cmdChange, rstTitles
    
    ' Print report of new data.
    Debug.Print _
        "Data in Titles table after executing the query"
    PrintOutput rstTitles

    ' Use the Connection object's execute method to 
    ' execute SQL statement to restore data. Trap for 
    ' errors, checking the Errors collection if necessary.
    On Error GoTo Err_Execute
    cnn1.Execute strSQLRestore, , adExecuteNoRecords
    On Error GoTo 0

    ' Retrieve the current data by requerying the recordset.
    rstTitles.Requery

    ' Print report of restored data.
    Debug.Print "Data after executing the query " & _
        "to restore the original information"
    PrintOutput rstTitles

    rstTitles.Close
    cnn1.Close
    
    Exit Sub
    
Err_Execute:

    ' Notify user of any errors that result from
    ' executing the query.
    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
    
    ' Run the specified Command object. Trap for 
    ' errors, checking the Errors collection if necessary.
    On Error GoTo Err_Execute
    cmdTemp.Execute
    On Error GoTo 0

    ' Retrieve the current data by requerying the recordset.
    rstTemp.Requery
    
    Exit Sub

Err_Execute:

    ' Notify user of any errors that result from
    ' executing the query.
    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)

    ' Enumerate Recordset.
    Do While Not rstTemp.EOF
        Debug.Print "  " & rstTemp!Title & _
            ", " & rstTemp!Type
        rstTemp.MoveNext
    Loop

End Sub

VBScript 版本

下列是使用 VBScript 撰寫,用於 Active Server Page (ASP) 的相同範例。若要檢視此全功能性範例,您必須使用安裝 IIS 時的資料來源 AdvWorks.mdb (位於 C:\InetPub\ASPSamp\AdvWorks),建立稱為 AdvWorks 的系統資料來源名稱 (Data Source Name,DSN)。這是一個 Microsoft Access 資料庫檔案。請使用 [尋找] 來尋找檔案 Adovbs.inc 並將其置於您計畫使用的目錄下。剪下及貼上下列程式碼至 [記事本] 或其它文字編輯器,並儲存為 Execute.asp 。您可以在任何客戶端瀏覽器上檢視結果。

<!-- #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 retrieved using Execute method of Connection and Command Objects-->
<% 
Set OBJdbConnection = Server.CreateObject("ADODB.Connection") 
OBJdbConnection.Open "AdvWorks" 
SQLQuery = "SELECT * FROM Customers" 
'First 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>

<!-- BEGIN column header row for Customer Table-->

<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>

<!--Display ADO Data from Customer Table-->
<% 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 and add to html table-->
<% 
RScustomerList.MoveNext 
Loop 
RScustomerList.Close

%>

</TABLE><HR>
<H4>Recordset Retrieved Using Command Object</H4>
<TABLE COLSPAN=8 CELLPADDING=5 BORDER=0>

<!-- BEGIN column header row for Product List Table-->

<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>

<!-- Display ADO Data 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 
'Remove objects from memory to free resources
RsProductList.Close
OBJdbConnection.Close
Set ObJdbCommand = Nothing
Set RsProductList = Nothing
Set OBJdbConnection = Nothing
%>
</TABLE></FONT></Center></BODY></HTML>