ADO初學者教程:ADO 添加記錄_ASP教程
推薦:ADO初學者教程:ADO 更新記錄我們可使用SQL的UPDATE來更新數(shù)據(jù)庫表中的某條記錄。 更新數(shù)據(jù)庫表中的記錄 我們希望更新Northwind數(shù)據(jù)中Customers表的某條記錄。首先我們需要創(chuàng)建一個表格,來列出Customers中的所有記錄。 htmlbody%set conn=Server.CreateObject(ADODB.Connection)conn.
我們可以使用SQL的INSERT INTO命令向數(shù)據(jù)庫中的表添加記錄。
向數(shù)據(jù)庫中的表添加記錄
我們希望向Northwind數(shù)據(jù)庫中的Customers表添加一條新的記錄。我們首先要創(chuàng)建一個表單,這個表單包含了我們希望選定數(shù)據(jù)的字段:
<html> <body> <form method="post" action="demo_add.asp"> <table> <tr> <td>CustomerID:</td> <td><input name="custid"></td> </tr><tr> <td>Company Name:</td> <td><input name="compname"></td> </tr><tr> <td>Contact Name:</td> <td><input name="contname"></td> </tr><tr> <td>Address:</td> <td><input name="address"></td> </tr><tr> <td>City:</td> <td><input name="city"></td> </tr><tr> <td>Postal Code:</td> <td><input name="postcode"></td> </tr><tr> <td>Country:</td> <td><input name="country"></td> </tr> </table> <br /><br /> <input type="submit" value="Add New"> <input type="reset" value="Cancel"> </form> </body> </html>
當用戶按下確認按鈕時,這個表單就會被送往名為"demo_add.asp"的文件。文件"demo_add.asp"中包含著可向Customers表添加一條新記錄的代碼:
<html>
<body>
<%
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("<h3>" & recaffected & " record added</h3>")
end if
conn.close
%>
</body>
</html>
重要事項
在您使用INSERT command命令時,請注意以下事項:
- 如果表含有一個主鍵,請確保向主鍵字段添加的值是唯一且非空的(否則,provider就不會追加此記錄,抑或發(fā)生錯誤)
- 如果表含有一個自動編號的字段,請不要在INSERT命令中涉及此字段(這個字段的值是由provider負責的)
關于無數(shù)據(jù)字段
在MS Access數(shù)據(jù)庫中,假如您將AllowZeroLength屬性設置為“Yes”,您可以在文本、超鏈接以及備忘字段輸入零長度的字符串("")。
注釋:并非所有的數(shù)據(jù)庫都支持零長度的字符串,因而當添加帶有空白字段的記錄時可能會產(chǎn)生錯誤。因此,檢查您使用的數(shù)據(jù)庫所支持的數(shù)據(jù)類型是很重要的。
分享:ADO初學者教程: ADO 刪除記錄我們可使用SQL的DELETE命令來刪除數(shù)據(jù)庫表中的某條記錄。 刪除表中的記錄 我們希望刪除Northwind數(shù)據(jù)庫的Customers表中的一條記錄。首先我們需要創(chuàng)建一個表格,來列出Customers中的所有記錄。 htmlbody%set conn=Server.CreateObject(ADODB.Connection)con
- 相關鏈接:
- 教程說明:
ASP教程-ADO初學者教程:ADO 添加記錄
。