クラシックASPでADODB.RecordSetオブジェクトを使って、データベースから検索するコード。IDとNAMEというカラムを持つ、TABLE1というテーブルのデータをすべて取得する。
' データベースへの接続 Dim connect : Set connect = CreateObject("ADODB.Connection") Call connect.Open("DSN=hoge;UID=xxx;PWD=yyy") If Err.Number <> 0 Then Call Response.Write(Err.Number & "<br>") Call Response.Write(Err.Source & "<br>") Call Response.Write(Err.Description & "<br>") Response.End End If Dim rs : Set rs = CreateObject("ADODB.RecordSet") Call rs.Open("SELECT * FROM TABLE1", connect) ' SQLの実行 If Err.Number <> 0 Then Call Response.Write(Err.Number & "<br>") Call Response.Write(Err.Source & "<br>") Call Response.Write(Err.Description & "<br>") Response.End End If ' クエリの結果が0件の場合 If rs.EOF Then Call Response.Write("0件" & "<br>") rs.Close Set rs = Nothing Response.End End If ' レコードセットの最後までループする Do Until rs.EOF Call Response.Write(rs("ID").value) Call Response.Write(":") Call Response.Write(rs("NAME").value) Call Response.Write("<br>") rs.MoveNext Loop ' レコードセットのクローズ rs.Close Set rs = Nothing ' コネクションのクローズ connect.Close Set connect = Nothing