powered by 25175.net如何操作Access数据库? 一、查找记录find.htm
Access数据库记录查找find.asp<% Option Explicit%><% Dim strman, strfind , objConn strman = Request.Form("man") Dim objRS Set objconn = Server.CreateObject("ADODB.Connection") set objRS = Server.CreateObject("ADODB.Recordset") objConn.Open("DRIVER={Microsoft Access Driver (*.mdb)}; DBQ=" & Server.MapPath("Man.mdb"))objRS.Open "Names", objConn, adOpenStatic, adLockReadOnly, adCmdTable strfind = "Name='" & strman & "'" objRS.Find strfind' 显示第一条相关记录 If objRS.EOF Then Response.Write "哦,没有找到您要查找的人!" & _ strman Else Response.Write "他/她的姓名是: " & strman & "" & _ "Id是:" & objRS("ID") & "
" & _ End If objRS.Close Set objRS = Nothing objConn.Close Set objConn = Nothing%> 二、过滤记录filter.asp<% Option Explicit%>
精彩春风之Access数据库记录过滤 <% Dim objRS, intLetter, strChar, strCriteria objConnSet objConn = Server.CreateObject("ADODB.Connection")Set objRS = Server.CreateObject("ADODB.Recordset") objConn.Open("DRIVER={Microsoft Access Driver (*.mdb)}; DBQ=" & Server.MapPath("Man.mdb")) objRS.Open "Names", objConn, adOpenStatic, adLockReadOnly, adCmdTable For intLetter = 1 To 26 strChar = Chr(intLetter+64) strCriteria = "Title LIKE '" & strChar & "*'" objRS.Filter = strCriteria If Not objRS.EOF Then Response.Write "
" & strChar & "
" & _ "
姓名 | ID |
" While Not objRS.EOF Response.Write "" & objRS("Name") & " | " & _ "" & objRS("ID") & " |
" objRS.MoveNext Wend Response.Write "
" End If Next objRS.Close Set objRS = NothingobjConn.Close Set objConn = Nothing%> 三、添加记录add.asp<% Option Explicit%>
精彩春风之Access数据库记录添加<% Dim objRS, intIDForNewRecord , objConnSet objConn = Server.CreateObject("ADODB.Connection")set objRS = Server.CreateObject("ADODB.Recordset") objConn.Open("DRIVER={Microsoft Access Driver (*.mdb)}; DBQ=" & Server.MapPath("Man.mdb"))' 连接数据库 objRS.Open "Names", objConn, adOpenStatic, adLockOptimistic, adCmdTable objRS.MoveLast intIDForNewRecord = objRS("ID") + 1 objRS.AddNew' 添加新记录 objRS("ID") = intIDForNewRecord objRS("Names") = "ManD" objRS.Update objRS.Close objRS.Open "SELECT * FROM Names WHERE ID=" & intIDForNewRecord, _ ObjConn, adOpenForwardOnly, adLockReadOnly, adCmdText If objRS.EOF Then Response.Write "噢,出错了!新记录没找到." Else Response.Write "恭喜,添加成功!新记录是:
" & _ "Name = '" & objRS("Name") & "'
" & _ "ID = '" & objRS("ID") & "'
" End If objRS.Close Set objRS = Nothing objConn.Close' 清空 Set objConn = Nothing%>四、更新记录和上面的“添加记录”一样,只需把“添加”命令改做“更新”就可以了:objRS.Update 五、删除记录This is the asp page, we will delete the record that we added ManD:<% Option Explicit%>
Access数据库记录删除<% Dim objRS, intIDForNewRecord, objConnSet objConn = Server.CreateObject("ADODB.Connection")set objRS = Server.CreateObject("ADODB.Recordset") objConn.Open("DRIVER={Microsoft Access Driver (*.mdb)}; DBQ=" & Server.MapPath("Man.mdb")) objRS.Open "Names", objConn, adOpenStatic, adLockOptimistic, adCmdTable objRS.Filter = "Name = ' ManD'" Response.Write "注意,以下记录将被删除:
" While Not objRs.EOF Response.Write objRS("ID") & "
" objRS.Delete objRS.MoveNext Wend objRS.Close Response.Write "
核实删除记录:
" objRS.Open "SELECT * FROM Names WHERE Name LIKE 'ManD'", _ objConn, adOpenForwardOnly, adLockReadOnly, adCmdText If objRS.EOF Then Response.Write "ManD的所有记录都已被从数据库删除!
" Else Response.Write "噢,删除失败!ManD的记录还在数据库里呢, " & _ "ID=" & objRS("ID") End If objRS.Close Set objRS = Nothing objConn.Close Set objConn = Nothing%>