简体   繁体   English

Connection.Close()错误ASP.NET

[英]Connection.Close() error ASP.NET

It is a simple data insert script I've found which is writing into access.db Here is the problem I'm having from the codes below: 这是我发现的一个简单的数据插入脚本,正在写入到access.db中。这是我在以下代码中遇到的问题:

Error: Line 16: Connection.Open() 错误:第16行:Connection.Open()

If you please let me know what exactly should I simply edit in the codes with, that would be great. 如果您能让我知道我应该简单地使用代码进行哪些编辑,那将是很棒的。

Many Thanks 非常感谢

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

jet.Oledb.4.0 doesnt work for 64Bit machine. jet.Oledb.4.0不适用于64位计算机。

You can use Microsoft.ACE.OLEDB.12.0 for 64 bit system. 您可以将Microsoft.ACE.OLEDB.12.0用于64位系统。 You can check is it 32 bit or 64 bit system. 您可以检查它是32位还是64位系统。 If 32 bit then use .JET.OLEDB else use ACE.OLEDB. 如果为32位,则使用.JET.OLEDB,否则使用ACE.OLEDB。

you are using the Jet.OLEDB.4.0 driver, which gives that error when run on 64 bit system, it is better to install the new driver Microsoft Access Database Engine 2010 Redistributable 您正在使用Jet.OLEDB.4.0驱动程序,在64位系统上运行时会出现该错误,最好安装新驱动程序Microsoft Access Database Engine 2010 Redistributable

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

also you'll need to change the connexion string from “Provider=Microsoft.Jet.OLEDB.4.0; 您还需要从“ Provider = Microsoft.Jet.OLEDB.4.0;”更改连接字符串。 “ to “Provider=Microsoft.ACE.OLEDB.12.0;” “改为“ Provider = Microsoft.ACE.OLEDB.12.0;”

reference from my blog : 64 bit version of 'Microsoft.Jet.OLEDB.4.0' Office 2007/2010 Jet drivers 来自我的博客的参考:64位版本的“ Microsoft.Jet.OLEDB.4.0” Office 2007/2010 Jet驱动程序

hope this helps. 希望这可以帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM