1 / 13

asp 与数据库基础

asp 与数据库基础. 教师:蒋义军 教学网址: http://jiangyj.cai.swufe.edu.cn 邮箱: air_amay@sina.com.cn 上机时间:双周 上机教室:学院机房. 一、 与数据库建立连接. ADO——ActiveX Data Objects 1 、创建 Connection 对象 Set cn = Server.CreateObject("ADODB.Connection")

devona
Download Presentation

asp 与数据库基础

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. asp与数据库基础 教师:蒋义军 教学网址:http://jiangyj.cai.swufe.edu.cn 邮箱:air_amay@sina.com.cn 上机时间:双周 上机教室:学院机房

  2. 一、与数据库建立连接 • ADO——ActiveX Data Objects • 1 、创建 Connection 对象 Set cn = Server.CreateObject("ADODB.Connection") 2、 使用 Connection 对象的 Open 方法打开数据库cn.Open "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=c:\asp\sia-cart.mdb“ 3 - 使用 Connection 对象的 Close 方法关闭连接Cn.close

  3. ADO——ActiveX Data Objects • 3 - 使用 Connection 对象的 Close 方法关闭连接Cn.close 4 - 将Connection 对象从内存中删除,以释放资源Set Cn = Nothing

  4. 二、检索数据库中的数据 • 通过创建 RecordSet 对象得到记录集 • 1 、创建 Recordset 对象 Set rs=Server.CreateObject("ADODB.Recordset") 2、将 Recordset 对象附加到连接 Cn Set rs. .ActiveConnection = Cn使用 Recordset 对象的 Open 方法打开记录集rs.Open StrSQL Recordset对象的Open方法原型: Open([Source],[ActiveConnection],[CursorType],[LockType],[Options]) SQL1= "select * from sia_admin" rs.open SQL1,cn,3,3,1

  5. 二、检索数据库中的数据 • 使用 Connection 对象的 Execute 方法得到记录集 • SQL1= "select * from sia_admin" • Set rs=Server.CreateObject("ADODB.Recordset") • set rs=cn.Execute(SQL1)

  6. 三、检索数据库中的数据 • 将得到的记录集显示到浏览器上Do While Not rs.EOF    Response.Write rs (0) & "  "  & rs (1) & "  "  & rs (2) & "  "  & "<BR>"    rs.MoveNext       Loop----------------------------------------------------------- • response.Write rs(1) • rs.movenext • response.Write("<br>") • response.Write rs(1)

  7. 四、以表格形式显示数据库表 • <table border="1"> <tr><td>工号</td><td>用户名</td><td>级别</td></tr> <% while(not rs.eof)%> <tr> <td><%=(rs("adminid"))%></td> <td><%=(rs("adminname"))%></td> <td><%=(rs("power"))%></td> </tr> <% rs.movenext() wend%> • </table>

  8. 从内存中清除对象 • rs.Close • Cn.Close Set rs= Nothing • Set Cn = Nothing

  9. 五、向数据库中添加新数据 • 使用Connection 对象的Execute 添加新数据<% • dim sql2 • sql2="insert into sia_admin(adminname,adminpassword,power) values('amay','airforce','董事长')“ • cn.execute(sql2) • %>

  10. 五、向数据库中添加新数据 • 使用Recordset 对象的AddNew 方法添加数据rs.CursorType = adOpenKeySetrs.LockType = adLockOptimistic rs.Open "sia_admin",Cn,,,adCmdTable rs.AddNew rs ("adminname") = “amay" rs ("adminpassword") = “1234" rs.Update

  11. 六、修改数据库中数据 • 使用Connection 对象的Execute修改数据<% • dim SQL3 • SQL3="update sia_admin • set adminname='amay'where adminname='admin'" • cn.execute(SQL3) • %>

  12. 七、删除数据库中数据 • 使用Connection 对象的Execute删除数据<% • dim SQL4 • SQL4="delete * from sia_admin where adminname='admin'" • cn.execute(SQL4) • %>

  13. 八、显示数据到页面 • Set rs= server.CreateObject("ADODB.Recordset") rs.Open " sia_admin ", Cn, , , adCmdTable Do While Not rs.EOF   Response.Write rs(0) & " " & rs(1) & " " & rs (2) & " " & "<BR>"   rst.MoveNext    Loop

More Related