已知ACCESS数据库名为tuihuo.mdb,表名为th_add,字段为th_name,th_time,th_beizhu用ASP写一个提交表单
时间: 2024-10-07 14:06:05 浏览: 36
在ASP中创建一个用于提交数据到ACCESS数据库的表单,首先需要设置HTML表单元素、服务器端脚本(如VBScript或ASP)来处理POST请求,并使用ADODB连接对象来操作数据库。下面是一个简单的例子,假设你想要插入一条新的记录:
```html
<!DOCTYPE html>
<html>
<head>
<title>添加记录</title>
<script runat="server">
Function InsertData()
Dim conn As ADODB.Connection
Set conn = New ADODB.Connection
' 数据库连接字符串
conn.ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\path\to\tuihuo.mdb;Persist Security Info=False;"
' SQL 插入语句
Dim sql As String
sql = "INSERT INTO th_add (th_name, th_time, th_beizhu) VALUES (?, ?, ?);"
' 准备SQL命令
Dim cmd As ADODB.Command
Set cmd = New ADODB.Command
cmd.ActiveConnection = conn
cmd.CommandText = sql
cmd.CommandType = adCmdText
' 获取表单输入值
Dim name As String
name = Request.Form("th_name")
Dim time As String
time = Request.Form("th_time")
Dim beizhu As String
beizhu = Request.Form("th_beizhu")
' 设置参数并执行插入
cmd.Parameters.Append(cmd.CreateParameter("th_name", adVarChar, adParamInput, 50, name))
cmd.Parameters.Append(cmd.CreateParameter("th_time", adVarChar, adParamInput, 255, time))
cmd.Parameters.Append(cmd.CreateParameter("th_beizhu", adVarChar, adParamInput, 255, beizhu))
On Error Resume Next
conn.Open
cmd.Execute
If Err.Number <> 0 Then
Response.Write "插入失败:" & Err.Description
Else
Response.Write "插入成功"
End If
On Error GoTo 0
cmd.Close
conn.Close
End Function
</script>
</head>
<body>
<form method="post" action="">
<label for="name">姓名:</label><br>
<input type="text" id="name" name="th_name"><br>
<label for="time">时间:</label><br>
<input type="text" id="time" name="th_time"><br>
<label for="beizhu">备注:</label><br>
<textarea id="beizhu" name="th_beizhu"></textarea><br>
<input type="submit" value="提交" onclick="InsertData(); return false;">
</form>
</body>
</html>
```
这个示例中,用户填写姓名、时间和备注后点击提交按钮,表单数据会被传递给服务器上的`InsertData`函数,该函数会将数据插入到`th_add`表中。
阅读全文