当前位置: 首页 > 网络学院 > 服务端脚本教程 > ADO > ADO添加记录

ADO
ADO DataTypes
ADO 摘要
ADO 实例

ADO 中的 ADO添加记录


出处:互联网   整理: 软晨网(RuanChen.com)   发布: 2009-03-01   浏览: 970 ::
收藏到网摘: n/a

We may use the SQL INSERT INTO command to add a record to a table in a database. 
我们可以使用INSERT INTO命令向一个数据库中的表中添加一条记录。


Add a Record to a Table in a Database
向一个数据库中的表中添加一条记录

We want to add a new record to the Customers table in the Northwind database. We first create a form that contains the fields we want to collect data from:
我们希望在“Northwind”数据库中的名为“Customers”表中添加一条新的记录。首先,我们先创建一个表,其中包含了所有的字段以及它所对应的数据:

<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>

When the user presses the submit button the form is sent to a file called "demo_add.asp". The "demo_add.asp" file contains the code that will add a new record to the Customers table:
将用户点击submit[提交]按钮后,这张表单将被发送到名为“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>


Important
重点

If you use the SQL INSERT command be aware of the following:
你在使用SQL INSERT命令时,需要注意以下几点:

  • If the table contains a primary key, make sure to append a unique, non-Null value to the primary key field (if not, the provider may not append the record, or an error occurs)
    如果该表单包含了一个私钥[primary key],那你必须要向这个私钥字段中添加一个独立的、非空值。否则,这条记录可能不会被添加,并且还会产生一个错误
  • If the table contains an AutoNumber field, do not include this field in the SQL INSERT command (the value of this field will be taken care of automatically by the provider)
    如果表单一个“AutoNumber”字段,那么请不要对这个字段使用INSERT命令(因为这个字段中的值会自动生成)。

What about Fields With no Data?
如果字段中不包含数据,那又会出现怎样的情况?

In a MS Access database, you can enter zero-length strings ("") in Text, Hyperlink, and Memo fields IF you set the AllowZeroLength property to Yes.
在MS[微软]的Access数据库中,如果你将AllowZeroLength属性设置为Yes,你就可以在文本、超链接和备注[Memo]字段中输入“零长度字符串("")”。

Note: Not all databases support zero-length strings and may cause an error when a record with blank fields is added. It is important to check what data types your database supports.
注意:并不是所有的数据库都支持“零长度字符串("")”的;当添加一个空的字符时,可能会出现错误。其中要的一点就是:你必须检查数据库支持的数据类型。

评论 (0) All

登陆 | 还没注册?