前面讲到了用command对象对数据库的操作,完成了增删查改,也可以使用ado的另外一个对象Recordset来完成对数据库的操作:
首先要创建RecordSet
View Code
<%
set conn=server.CreateObject("adodb.connection")
connstr="provider=sqloledb;data source=(local)\sky;initial catalog=lszx;user id=sa;password=sa;"
conn.open connstr
set rs=server.CreateObject("adodb.RecordSet")
strsql="select * from tb_person"
rs.open strsql,conn,2,3
%>
set conn=server.CreateObject("adodb.connection")
connstr="provider=sqloledb;data source=(local)\sky;initial catalog=lszx;user id=sa;password=sa;"
conn.open connstr
set rs=server.CreateObject("adodb.RecordSet")
strsql="select * from tb_person"
rs.open strsql,conn,2,3
%>
技巧:查询就是1,1
增删改就用1,3或2,3(1 执行操作后记录集里只保存当前记录,2 是全部 )
其次就可以调用RecordSet的方法(如AddNew,update,delete)
AddNew
View Code
rs.addnew()
rs("userName")=request("txt_username")
rs("sex")=request("txt_sex")
rs("age")=request("txt_age")
rs("aihao")=request("txt_aihao")
rs("userName")=request("txt_username")
rs("sex")=request("txt_sex")
rs("age")=request("txt_age")
rs("aihao")=request("txt_aihao")
rs.update ‘更新记录集
update
View Code
rs("username")=request("txt_username")
rs("sex")=request("txt_sex")
rs("age")=request("txt_age")
rs("aihao")=request("txt_aihao")
rs.update
response.Write("<script language='javascript'>alert('数据修改成功!');location.href='user_list2.asp'</script>")
rs("sex")=request("txt_sex")
rs("age")=request("txt_age")
rs("aihao")=request("txt_aihao")
rs.update
response.Write("<script language='javascript'>alert('数据修改成功!');location.href='user_list2.asp'</script>")
循环读取数据
while not rs.eof
rs("")
rs.movenext
wend
记录数
rs.RecordCount