Menu
×
   ❮     
HTML CSS JAVASCRIPT SQL PYTHON JAVA PHP HOW TO W3.CSS C C++ C# BOOTSTRAP REACT MYSQL JQUERY EXCEL XML DJANGO NUMPY PANDAS NODEJS R TYPESCRIPT ANGULAR GIT POSTGRESQL MONGODB ASP AI GO KOTLIN SASS VUE DSA GEN AI SCIPY AWS CYBERSECURITY DATA SCIENCE
     ❯   

ADO 添加记录


我们可以使用 SQL INSERT INTO 命令将记录添加到数据库中的表中。 


将记录添加到数据库中的表

我们想要将新记录添加到 Northwind 数据库中的 Customers 表。 我们首先创建一个包含我们想要收集数据的字段的表单。

<html>
<body>

<form method="post" action="demo_add.asp">
<table>
<tr>
<td>CustomerID:</td>
<td><input name="custid"></td>
</tr><tr>
<td>公司名称:</td>
<td><input name="compname"></td>
</tr><tr>
<td>联系人姓名:</td>
<td><input name="contname"></td>
</tr><tr>
<td>地址:</td>
<td><input name="address"></td>
</tr><tr>
<td>城市:</td>
<td><input name="city"></td>
</tr><tr>
<td>邮政编码:</td>
<td><input name="postcode"></td>
</tr><tr>
<td>国家:</td>
<td><input name="country"></td>
</tr>
</table>
<br><br>
<input type="submit" value="添加新记录">
<input type="reset" value="取消">
</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("没有更新权限!")
else
  Response.Write("<h3>" & recaffected & " 条记录已添加</h3>")
end if
conn.close
%>

</body>
</html>

重要

如果您使用 SQL INSERT 命令,请注意以下事项

  • 如果表包含主键,请确保将唯一的非空值附加到主键字段(如果没有,提供程序可能不会附加记录,或发生错误)。
  • 如果表包含 AutoNumber 字段,请不要在 SQL INSERT 命令中包含此字段(此字段的值将由提供程序自动处理)。

没有数据的字段怎么办?

在 MS Access 数据库中,如果您将 AllowZeroLength 属性设置为 Yes,您可以在 Text、Hyperlink 和 Memo 字段中输入零长度字符串 ("")。

注意:并非所有数据库都支持零长度字符串,这可能会导致添加具有空白字段的记录时出错。 重要的是检查您的数据库支持哪些数据类型。


×

Contact Sales

If you want to use W3Schools services as an educational institution, team or enterprise, send us an e-mail:
[email protected]

Report Error

If you want to report an error, or if you want to make a suggestion, send us an e-mail:
[email protected]

W3Schools is optimized for learning and training. Examples might be simplified to improve reading and learning. Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. While using W3Schools, you agree to have read and accepted our terms of use, cookie and privacy policy.

Copyright 1999-2024 by Refsnes Data. All Rights Reserved. W3Schools is Powered by W3.CSS.