下面我们就通过一个实例来说明这种技术的使用方法(NorthWind.mdb是Microsoft Access97自带的一个数据库,文件adovbs.inc可在C:\Program Files\Common Files\System\ADO下找到):
复制代码 代码如下:
<% @LANGUAGE = VBScript %>
<!--#include file="adovbs.inc"-->
<%
Response.Expires = 0
Dim Cnn,objRS, strOut, strQ, strC
StrC= "Driver={Microsoft Access Driver (*.mdb)}; DBQ=" & Server.MapPath("\asp24") & "\NorthWind.mdb;"
'建立连接
Set Cnn = Server.CreateObject("ADODB.Connection")
Cnn.Open StrC
'创建Recordset对象
Set objRS = Server.CreateObject("ADODB.Recordset")
objRS.CursorLocation =adUseClient
objRS.CursorType = adOpenStatic
objRS.LockType = adLockOptimistic
strQ = "SELECT 运货商ID, 公司名称, 电话 FROM 运货商 "
objRS.Open strQ, Cnn, , , adCmdText
Set objRS.ActiveConnection = Nothing '断开记录集
Cnn.Close '关闭连接
Set Cnn = Nothing
Response.Write "<HTML><BODY>"
'下面使用断开的记录集
Do While (Not objRS.EOF)
strOut = objRS("运货商ID") & ", " & objRS("公司名称") & ", " & objRS("电话")
Response.Write Server.HTMLEncode(strOut) & "<BR>"
objRS.MoveNext
Loop
Response.Write "<BR>准备新增或插入记录: "
'若需要更新数据库, 则要重新建立连接
Set Cnn = Server.CreateObject("ADODB.Connection")
Cnn.Open strC
Set objRS.ActiveConnection = Cnn
objRS.Filter = "公司名称 = '吴丰'"
If objRS.EOF Then
objRS.AddNew
objRS("公司名称") = "吴丰"
objRS("电话") = "571-7227298"
objRS.Update
Response.Write "符合该条件的记录不存在, 则新增.<BR>"
Else
objRS("电话") = "571-7227071"
Response.Write "符合该条件的记录存在, 则 Update.<BR>"
objRS.Update
End If
Set objRS.ActiveConnection = Nothing
Cnn.close
Set Cnn = Nothing
objRS.Close
Set objRS = Nothing
Response.Write "</BODY></HTML>"
%>