1

这是我发现的一个简单的数据插入脚本,它正在写入 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>
4

2 回答 2

0

jet.Oledb.4.0 不适用于 64 位机器。

您可以将 Microsoft.ACE.OLEDB.12.0 用于 64 位系统。您可以检查它是 32 位还是 64 位系统。如果是 32 位,则使用 .JET.OLEDB,否则使用 ACE.OLEDB。

于 2012-12-17T23:27:50.490 回答
-1

您正在使用 Jet.OLEDB.4.0 驱动程序,在 64 位系统上运行时会出现该错误,最好安装新驱动程序 Microsoft Access Database Engine 2010 Redistributable

http://www.microsoft.com/download/en/details.aspx?id=13255

您还需要从“Provider=Microsoft.Jet.OLEDB.4.0;”更改连接字符串;“到”Provider=Microsoft.ACE.OLEDB.12.0;”</p>

来自我的博客的参考:'Microsoft.Jet.OLEDB.4.0' Office 2007/2010 Jet 驱动程序的 64 位版本

希望这可以帮助。

于 2012-12-17T23:35:36.860 回答