Execute、Requery、Clear メソッドの例 (VBScript)

この例では、Command オブジェクトの Execute メソッドを実行する場合と Connection オブジェクトの Execute メソッドを実行する場合を示します。またここでは、Recordset オブジェクト内の最新のデータを取得する Requery メソッド、および Errors コレクションの内容をクリアする Clear メソッドも使用します。このプロシージャを実行するには、ExecuteCommand と PrintOutput プロシージャが必要です。

次の例は、Active Server Pages (ASP) で使用してください。この例を完全に機能させて表示するには、SDK と共にインストールされるデータ ソース AdvWorks.mdb が、C:\mssdk\samples\dataaccess\rds に置かれている必要があります。これは、Microsoft Access のデータベース ファイルです。

検索機能を使ってファイル Adovbs.inc を検索し、使用するディレクトリに置きます。次のコードをコピーして、メモ帳または別のテキスト エディタに貼り付け、「Execute.asp」という名前で保存します。結果は、任意のクライアント ブラウザで表示できます。

<%@ Language=VBScript %>
<!-- #Include file="ADOVBS.INC" -->
<HTML>
<HEAD>
<TITLE>ADO Execute Method</TITLE>
<STYLE>
<!--
BODY {
    font-family: "MS SANS SERIF",sans-serif;
     }
.thead1 {
    background-color: #008080; 
    font-family: 'Arial Narrow','Arial',sans-serif; 
    font-size: x-small;
    color: white;
    }
.thead2 {
    background-color: #800000; 
    font-family: 'Arial Narrow','Arial',sans-serif; 
    font-size: x-small;
    color: white;
    }
.tbody { 
    text-align: center;
    background-color: #f7efde;
    font-family: 'Arial Narrow','Arial',sans-serif; 
    font-size: x-small;
     }
-->
</STYLE>
</HEAD>

<BODY> 
<H3>ADO Execute Method</H3>
<HR>
<H4>Recordset Retrieved Using Connection Object</H4>
<!--- Recordsets retrieved using Execute method of Connection and Command Objects-->
<% 
sDsrc="c:\mssdk\samples\dataaccess\rds\advworks.mdb"
sConnStr="Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & sDsrc

Set OBJdbConnection = Server.CreateObject("ADODB.Connection") 
OBJdbConnection.Open sConnStr 
SQLQuery = "SELECT * FROM Customers" 
'1st Recordset RSCustomerList using conn execute mthd
Set RSCustomerList = OBJdbConnection.Execute(SQLQuery) 

Set OBJdbCommand = Server.CreateObject("ADODB.Command")
OBJdbCommand.ActiveConnection = OBJdbConnection
SQLQuery2 = "SELECT * From Products"
OBJdbCommand.CommandText = SQLQuery2
'2nd Recordset RSProductList using conn execute mthd
Set RSProductList = OBJdbCommand.Execute
%>
<TABLE COLSPAN=8 CELLPADDING=5 BORDER=0 ALIGN=CENTER>
<!-- BEGIN column header row for Customer Table-->
<TR CLASS=thead1>
  <TH>Company Name</TH>
  <TH>Contact Name</TH>
  <TH>E-mail address</TH>
  <TH>City</TH>
  <TH>State/Province</TH>
</TR>

<!--Display ADO Data from Customer Table-->
<% 
Do While Not RScustomerList.EOF %>
  <TR CLASS=tbody>
  <TD> 
  <%= RSCustomerList("CompanyName")%> 
  </TD>
  <TD>
  <%= RScustomerList("ContactLastName") & ", " %> 
  <%= RScustomerList("ContactFirstName") %> 
  </TD>
  <TD>
  <%= RScustomerList("ContactLastName")%> 
  </TD>
  <TD> 
  <%= RScustomerList("City")%> 
  </TD>
  <TD> 
  <%= RScustomerList("StateOrProvince")%> 
  </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 CELLPADDING=5 BORDER=0 ALIGN=CENTER WIDTH="80%">

<!-- BEGIN column header row for Product List Table-->
<TR CLASS=thead2>
  <TH>Product Type</TH>
  <TH>Product Name</TH>
  <TH>Product Description</TH>
  <TH>Unit Price</TH>
</TR>

<!-- Display ADO Data Product List-->
<% Do While Not RsProductList.EOF %>
  <TR CLASS=tbody>
  <TD> 
  <%= RsProductList("ProductType")%> 
  </TD>
  <TD>
  <%= RsProductList("ProductName")%>  
  </TD>
  <TD>
   <%= RsProductList("ProductDescription")%> 
  </TD>
  <TD> 
  <%= RsProductList("UnitPrice")%> 
  </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>
</BODY>
</HTML>