目录
一、ADO 排序记录
1.对数据进行排序
2.根据指定的字段名处对记录进行升序排序
3.根据指定的字段名处对记录进行降序排序
4.让用户来选择根据哪列进行排序
二、ADO 添加记录
1.向数据库中的表添加记录
2.使用 INSERT command 命令时的注意事项
3.无数据字段
三、ADO 更新记录
1.更新数据库表中的记录
使用SQL来规定如何对记录集中的数据进行排序。
显示 "Customers" 表中的"Companyname"和"Contactname"字段,并根据"Companyname"进行排序(请记得用.asp为后缀保存):
<%
set conn=Server.CreateObject("ADODB.Connection")
conn.Provider="Microsoft.Jet.OLEDB.4.0"
conn.Open "c:/webdata/northwind.mdb"
set rs = Server.CreateObject("ADODB.recordset")
sql="SELECT Companyname, Contactname FROM
Customers ORDER BY CompanyName"
rs.Open sql, conn
%>
<%for each x in rs.Fields
response.write("
" & x.name & " ")next%>
<%do until rs.EOF%>
<%for each x in rs.Fields%>
<%Response.Write(x.value)%> <%next
rs.MoveNext%>
<%loop
rs.close
conn.close%>
<%
set conn=Server.CreateObject("ADODB.Connection")
conn.Provider="Microsoft.Jet.OLEDB.4.0"
conn.Open(Server.Mappath("/db/northwind.mdb"))
set rs = Server.CreateObject("ADODB.recordset")
sql="SELECT Companyname, Contactname FROM Customers ORDER BY CompanyName"
rs.Open sql, conn
%>
<%for each x in rs.Fields
response.write("
" & x.name & " ")next%>
<%do until rs.EOF%>
<%for each x in rs.Fields%>
<%Response.Write(x.value)%> <%next
rs.MoveNext%>
<%loop
rs.close
conn.close
%>
<%
set conn=Server.CreateObject("ADODB.Connection")
conn.Provider="Microsoft.Jet.OLEDB.4.0"
conn.Open(Server.Mappath("/db/northwind.mdb"))
set rs = Server.CreateObject("ADODB.recordset")
sql="SELECT Companyname, Contactname FROM Customers ORDER BY CompanyName DESC"
rs.Open sql, conn
%>
<%for each x in rs.Fields
response.write("
" & x.name & " ")next%>
<%do until rs.EOF%>
<%for each x in rs.Fields%>
<%Response.Write(x.value)%> <%next
rs.MoveNext%>
<%loop
rs.close
conn.close
%>
<%
if request.querystring("sort")<>"" then
sort=request.querystring("sort")
else
sort="companyname"
end if
set conn=Server.CreateObject("ADODB.Connection")
conn.Provider="Microsoft.Jet.OLEDB.4.0"
conn.Open(Server.Mappath("/db/northwind.mdb"))
set rs=Server.CreateObject("ADODB.recordset")
sql="SELECT Companyname,Contactname FROM Customers ORDER BY " & sort
rs.Open sql,conn
do until rs.EOF
response.write("
") ")for each x in rs.Fields
response.write("
" & x.value & " ")next
rs.MoveNext
response.write("
loop
rs.close
conn.close
%>
使用 SQL 的 INSERT INTO 命令向数据库中的表添加记录。
向 Northwind 数据库中的 Customers 表添加一条新的记录。
首先要创建一个表单,这个表单包含了从中搜集数据的输入域:
当用户按下确认按钮时,这个表单就会被送往名为 "demo_add.asp" 的文件。文件 "demo_add.asp" 中含有可向 Customers 表添加一条新记录的代码:
<%
set conn=Server.CreateObject("ADODB.Connection")
conn.Provider="Microsoft.Jet.OLEDB.4.0"
conn.Open "c:/webdata/northwind.mdb"
sql="INSERT INTO customers (customerID,companyname,"
sql=sql & "contactname,address,city,postalcode,country)"
sql=sql & " VALUES "
sql=sql & "('" & Request.Form("custid") & "',"
sql=sql & "'" & Request.Form("compname") & "',"
sql=sql & "'" & Request.Form("contname") & "',"
sql=sql & "'" & Request.Form("address") & "',"
sql=sql & "'" & Request.Form("city") & "',"
sql=sql & "'" & Request.Form("postcode") & "',"
sql=sql & "'" & Request.Form("country") & "')"
on error resume next
conn.Execute sql,recaffected
if err<>0 then
Response.Write("No update permissions!")
else
Response.Write("
" & recaffected & " record added
")end if
conn.close
%>
1.如果表含有一个主键,请确保向主键字段添加的值是唯一且非空的(否则,provider 就不会追加此记录,亦或发生错误)
2.如果表含有一个自动编号的字段,请不要在 INSERT 命令中涉及此字段(这个字段的值是由 provider 负责的)
在 MS Access 数据库中,将 AllowZeroLength 属性设置为 “Yes”,就可以在文本、超链接以及备忘字段输入零长度的字符串 ("")。
注:并非所有的数据库都支持零长度的字符串。
使用 SQL 的 UPDATE 来更新数据库表中的某条记录。
更新 Northwind 数据中 Customers 表的某条记录。
首先创建一个表格,来列出 Customers 中的所有记录。
<%
set conn=Server.CreateObject("ADODB.Connection")
conn.Provider="Microsoft.Jet.OLEDB.4.0"
conn.Open "c:/webdata/northwind.mdb"
set rs=Server.CreateObject("ADODB.Recordset")
rs.open "SELECT * FROM customers",conn
%>
List Database
<%
for each x in rs.Fields
response.write("
" & ucase(x.name) & " ")next
%>
<% do until rs.EOF %>
<%rs.MoveNext%>
<%
loop
conn.close
%>
如果用户点击 "customerID" 列中的按钮,会打开一个新文件 "demo_update.asp"。此文件包含了创建输入域的源代码,这些输入域基于数据库中记录的字段,同时也含有一个保存修改的“更新按钮”:
Update Record
<%
set conn=Server.CreateObject("ADODB.Connection")
conn.Provider="Microsoft.Jet.OLEDB.4.0"
conn.Open "c:/webdata/northwind.mdb"
cid=Request.Form("customerID")
if Request.form("companyname")="" then
set rs=Server.CreateObject("ADODB.Recordset")
rs.open "SELECT * FROM customers WHERE customerID='" & cid & "'",conn
%>
<%
else
sql="UPDATE customers SET "
sql=sql & "companyname='" & Request.Form("companyname") & "',"
sql=sql & "contactname='" & Request.Form("contactname") & "',"
sql=sql & "address='" & Request.Form("address") & "',"
sql=sql & "city='" & Request.Form("city") & "',"
sql=sql & "postalcode='" & Request.Form("postalcode") & "',"
sql=sql & "country='" & Request.Form("country") & "'"
sql=sql & " WHERE customerID='" & cid & "'"
on error resume next
conn.Execute sql
if err<>0 then
response.write("No update permissions!")
else
response.write("Record " & cid & " was updated!")
end if
end if
conn.close
%>