这是我发现的一个简单的数据插入脚本,它正在写入 access.db 这是我在以下代码中遇到的问题:
错误:第 16 行:Connection.Open()
如果你让我知道我应该在代码中简单地编辑什么,那就太好了。
非常感谢
<%@ Page Language="VB" Debug="true"%>
<%@ Import Namespace="System.Data.Oledb" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs)
If Page.IsPostBack = False Then
Label1.Visible = False
End If
End Sub
Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs)
Dim Connection As New OleDbConnection("Provider=Microsoft.Jet.Oledb.4.0; Data Source=C:\inetpub\wwwroot\Thinkdebt\customers.mdb")
Connection.Open()
Dim Command As New OleDbCommand("INSERT INTO tblCustomers(FirstName," & _
"LastName)VALUES(@FirstName,@LastName)", Connection)
Command.Parameters.Add(New OleDbParameter("@FirstName", TextBox1.Text))
Command.Parameters.Add(New OleDbParameter("@LastName", TextBox2.Text))
Command.ExecuteNonQuery()
Connection.Close()
Label1.Text = "Record inserted."
Label1.Visible = True
TextBox1.Text = ""
TextBox2.Text = ""
End Sub
</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head id=Head1 runat="server">
<title>ASP.NET Form to Database Sample</title>
</head>
<body>
<div>
<h1>ASP.NET Form to Database</h1>
<form id="form1" runat="server">
First name:<asp:TextBox ID="TextBox1" runat="server" Text=""></asp:TextBox>
<asp:RequiredFieldValidator ID="RequiredFieldValidator1" ControlToValidate="TextBox1" runat="server" ErrorMessage="Please enter a first name"></asp:RequiredFieldValidator><br /><br />
Last name:<asp:TextBox ID="TextBox2" runat="server" Text=""></asp:TextBox>
<asp:RequiredFieldValidator ID="RequiredFieldValidator2" ControlToValidate="TextBox2" runat="server" ErrorMessage="Please enter a last name"></asp:RequiredFieldValidator><br /><br />
<asp:Button ID=Button1 runat="server" Text="Insert Record" OnClick="Button1_Click" />
<br />
<asp:Label ID=Label1 runat="server" Text=""></asp:Label>
</form>
</div>
</body>
</html>