ADO初學(xué)者教程:ADO 添加記錄_ASP教程
推薦:ADO初學(xué)者教程: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>
當(dāng)用戶按下確認(rèn)按鈕時,這個表單就會被送往名為"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>
重要事項(xiàng)
在您使用INSERT command命令時,請注意以下事項(xiàng):
- 如果表含有一個主鍵,請確保向主鍵字段添加的值是唯一且非空的(否則,provider就不會追加此記錄,抑或發(fā)生錯誤)
- 如果表含有一個自動編號的字段,請不要在INSERT命令中涉及此字段(這個字段的值是由provider負(fù)責(zé)的)
關(guān)于無數(shù)據(jù)字段
在MS Access數(shù)據(jù)庫中,假如您將AllowZeroLength屬性設(shè)置為“Yes”,您可以在文本、超鏈接以及備忘字段輸入零長度的字符串("")。
注釋:并非所有的數(shù)據(jù)庫都支持零長度的字符串,因而當(dāng)添加帶有空白字段的記錄時可能會產(chǎn)生錯誤。因此,檢查您使用的數(shù)據(jù)庫所支持的數(shù)據(jù)類型是很重要的。
分享:ADO初學(xué)者教程: ADO 刪除記錄我們可使用SQL的DELETE命令來刪除數(shù)據(jù)庫表中的某條記錄。 刪除表中的記錄 我們希望刪除Northwind數(shù)據(jù)庫的Customers表中的一條記錄。首先我們需要創(chuàng)建一個表格,來列出Customers中的所有記錄。 htmlbody%set conn=Server.CreateObject(ADODB.Connection)con
- asp FSO 讀寫文件本文件實(shí)現(xiàn)代碼
- asp中isNull、isEmpty和空字符串的區(qū)別
- asp獲取用戶真實(shí)IP地址的方法
- asp連接sqlserver數(shù)據(jù)庫實(shí)現(xiàn)代碼
- asp中正則表達(dá)式過濾html代碼函數(shù)
- asp中g(shù)et post提交表單區(qū)別
- 網(wǎng)頁模板:ASP內(nèi)建對象Request
- xmlhttp的open方法使用詳解
- ASP的常用的自定義函數(shù)大全
- asp中用for循環(huán)的一個小技巧
- eWebEditor v3.8 列目錄
- ASP無組件分頁實(shí)現(xiàn)思路及代碼
- 相關(guān)鏈接:
- 教程說明:
ASP教程-ADO初學(xué)者教程:ADO 添加記錄
。