令中国铁道出版社 84数据库基 841 SQL Server四大基本操作 di m conn. rs Set Conn= Server CreateObject("ADODB Connection Conn Open"Driver(SQL Server); Database=northwind Server= Local); uid=sa pwd Set rs=Server CreateObject("ADODB. Recordset") SOL =Customers rsopen SQL, conn, 3, 2 说明:在 SQL Server中有一种特 rs AddNew 殊的数值字段,它具有自动增加 rs(" Customerld'")="海河 某固定值(例如每次加1)的功能(类 rs("Company Name 摄影艺术会似产生流水号),切记此种字段在 rs(ContactName) rs(" ContactTitle'")="摄影记者" 添加时,不须特别指定该字段的 rs("Cty")="天津" 值,因为系统会自动赋予字段应 rs Update 有的值。数据库字段若有默认值, et rs= Nothing 使用本操作时字段的值将被初始 Set Conn= nothing 化,因此可能会导致默认值发生 错误的情况 Insert complete 26
26 8.4 数据库基本操作 • 8.4.1 SQL Server四大基本操作 <% dim Conn, rs Set Conn = Server.CreateObject("ADODB.Connection") Conn.Open "Driver={SQL Server};Database=northwind; Server=(Local); uid=sa;pwd=" Set rs = Server.CreateObject("ADODB.Recordset") SQL = "Customers" rs.open SQL, conn, 3, 2 rs.AddNew rs("CustomerID") = "海河" rs("CompanyName") = "海河摄影艺术会" rs("ContactName") = "高笑明" rs("ContactTitle") = "摄影记者" rs("City") = "天津" rs.Update Set rs = Nothing Set Conn = Nothing %> Insert complete. 说明:在SQL Server中有一种特 殊的数值字段,它具有自动增加 某固定值(例如每次加1)的功能(类 似产生流水号),切记此种字段在 添加时,不须特别指定该字段的 值,因为系统会自动赋予字段应 有的值。数据库字段若有默认值, 使用本操作时字段的值将被初始 化,因此可能会导致默认值发生 错误的情况
令中国铁道出版社 84数据库基 841 SQL Server四大基本操作 2.修改 要修改 SQL Server中的数据,首先我们必须先找 出该条记录,在确认数据无误后,更新表相关字段内 容之后,使用 Update方法将修改后的数据写回数据库。 更正数据的ASP程序代码范例ch8-3asp)见下页:
27 8.4 数据库基本操作 • 8.4.1 SQL Server四大基本操作 2. 修改 要修改SQL Server中的数据,首先我们必须先找 出该条记录,在确认数据无误后,更新表相关字段内 容之后,使用Update方法将修改后的数据写回数据库。 更正数据的ASP程序代码范例(ch8-3.asp)见下页:
令中国铁道出版社 84数据库基 841 SQL Server四大基本操作 dim Conn rs Set Conn= Server CreateObject("ADODB Connection Conn Open"Driver-iSQL Server); Database=northwind Server= Local); uid=sa; pwd Set rs= Server CreateObject("ADODB. Recordset") SQL ="SELECT* FROM Customers WHERE CustomerID=ALFKI rsopen SQL, conn, 3, 2 if not rs Eof then rs("City")="柏林( Berlin) rs Update End If Set rs- Nothing Set Conn= nothing Update complete 28
28 8.4 数据库基本操作 • 8.4.1 SQL Server四大基本操作 <% dim Conn, rs Set Conn = Server.CreateObject("ADODB.Connection") Conn.Open "Driver={SQL Server};Database=northwind; Server=(Local);uid=sa;pwd=" Set rs = Server.CreateObject("ADODB.Recordset") SQL = "SELECT * FROM Customers WHERE CustomerID='ALFKI'" rs.open SQL, conn, 3, 2 if not rs.Eof Then rs("City") = "柏林(Berlin)" rs.Update End If Set rs = Nothing Set Conn = Nothing %> Update complete
令中国铁道出版社 84数据库基 841 SQL Server四大基本操作 3.删除 将数据库中无用的数据删除,以免占用数据库的空间。删除数 据的范例(ch8-4asp如下: dim Conn rs et Conn= Server CreateObject("ADODB Connection Conn Open"Driver(SQL Server); Database=northwind; Server= Local) uid=sa pwd= Set rs= Server CreateObject("ADODB. Recordset") SQL ="SELECT FROM Customers WHERE CustomerID=iBiB] rsopen SQL, conn, 3, 2 If not rs eof then rs Delete e Response. Write("Delete complete. " Response. Write("Record not found! " End If Set rs= nothi Set conn nothing 29
29 8.4 数据库基本操作 • 8.4.1 SQL Server四大基本操作 3. 删除 将数据库中无用的数据删除,以免占用数据库的空间。删除数 据的范例(ch8-4.asp)如下: <% dim Conn, rs Set Conn = Server.CreateObject("ADODB.Connection") Conn.Open "Driver={SQL Server};Database=northwind; Server=(Local);uid=sa;pwd=" Set rs = Server.CreateObject("ADODB.Recordset") SQL = "SELECT * FROM Customers WHERE CustomerID='海河'" rs.open SQL, conn, 3, 2 If not rs.Eof Then rs.Delete Response.Write("Delete complete.") Else Response.Write("Record not found !") End If Set rs = Nothing Set Conn = Nothing %>
令中国铁道出版社 84数据库基 841 SQL Server四大基本操作 4.查询 在数据库的 SQL Command中,查询( SELECT)的语法是最复杂 的。它可以变化的地方最多,应用的层面也最为广泛,其中 SELECT FROM是 SQL Command中最简单也最常用的查询命令。 例如,我们要列出所有北风数据库中 Customers表的 CustomerID及 Company Name两个字段,并将输出字段以表格的方式输出(奇数行 与偶数行颜色不同)。ASP的程序代码(ch8-5asp如下 dim Conn. rs Set Conn= Server CreateObject("ADODB Connection Conn Open"Driver=(SQL Server); Database=northwind Server=(Local); uid=sa pwd= Set rs= Server CreateObject("ADODB. Recordset SQL ="SELECT CustomerID, Company Name FROM Customers rsopen SQL, conn, 3, 2 Response. Write(<TABLE BORDER=I Response Write(<tR bgcolor=cyan> 30
30 8.4 数据库基本操作 • 8.4.1 SQL Server四大基本操作 4. 查询 在数据库的SQL Command中,查询(SELECT)的语法是最复杂 的。它可以变化的地方最多,应用的层面也最为广泛,其中 SELECT...FROM是SQL Command中最简单也最常用的查询命令。 例如,我们要列出所有北风数据库中Customers表的CustomerID及 CompanyName两个字段,并将输出字段以表格的方式输出(奇数行 与偶数行颜色不同)。ASP的程序代码(ch8-5.asp)如下: <% dim Conn, rs Set Conn = Server.CreateObject("ADODB.Connection") Conn.Open "Driver={SQL Server};Database=northwind; Server=(Local);uid=sa;pwd=" Set rs = Server.CreateObject("ADODB.Recordset") SQL = “SELECT CustomerID,CompanyName FROM Customers” rs.open SQL, conn, 3, 2 Response.Write("<TABLE BORDER=1>") Response.Write("<TR BGCOLOR=cyan>")