OleDbConnection - ExecuteNonQuery 需要一个打开且可用的连接。连接当前状态已关闭

发布于 2024-12-19 12:28:25 字数 1821 浏览 1 评论 0原文

我在这里做错了什么?

using System.Data;
using System.Data.OleDb;

namespace myProject.Account
{
    public class DbManager
    {

        private OleDbConnection OpenDbConnection()
        {
            string connectionString = GetConnectionString();
            return new OleDbConnection {ConnectionString = connectionString};
        }

        private string GetConnectionString()
        {
            return "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\\myDataBase.accdb";
        }

       public void InsertUser(string name, string loginName, string password)
       {
            OleDbConnection conn = OpenDbConnection();

            OleDbCommand command = new OleDbCommand(
                 "INSERT INTO tblUser (UserName, LoginName, Password) VALUES (@name,@login,@pwd)",
                 Conn);

            command.Parameters.Add("@name", OleDbType.VarChar).Value = name;
            command.Parameters.Add("@login", OleDbType.VarChar).Value = loginName;
            command.Parameters.Add("@pwd", OleDbType.VarChar).Value = password;
            command.ExecuteNonQuery();
       }
   }
}

我收到这个错误:

ExecuteNonQuery 需要一个打开且可用的连接。连接的当前状态已关闭。
描述:执行当前 Web 请求期间发生未处理的异常。请查看堆栈跟踪以了解更多信息 有关错误及其在代码中的来源的信息。

异常详细信息:System.InvalidOperationException:ExecuteNonQuery 需要打开且可用的连接。连接的当前状态已关闭。

来源错误:

第 31 行:command.ExecuteNonQuery();

我尝试查看其他一些线程,但没有一个有帮助:

ExecuteNonQuery 需要一个开放且可用的连接。连接的当前状态已关闭

MS Access DB 不执行后保存更改(C#)

What am I doing wrong here?

using System.Data;
using System.Data.OleDb;

namespace myProject.Account
{
    public class DbManager
    {

        private OleDbConnection OpenDbConnection()
        {
            string connectionString = GetConnectionString();
            return new OleDbConnection {ConnectionString = connectionString};
        }

        private string GetConnectionString()
        {
            return "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\\myDataBase.accdb";
        }

       public void InsertUser(string name, string loginName, string password)
       {
            OleDbConnection conn = OpenDbConnection();

            OleDbCommand command = new OleDbCommand(
                 "INSERT INTO tblUser (UserName, LoginName, Password) VALUES (@name,@login,@pwd)",
                 Conn);

            command.Parameters.Add("@name", OleDbType.VarChar).Value = name;
            command.Parameters.Add("@login", OleDbType.VarChar).Value = loginName;
            command.Parameters.Add("@pwd", OleDbType.VarChar).Value = password;
            command.ExecuteNonQuery();
       }
   }
}

.

I got this error:

ExecuteNonQuery requires an open and available Connection. The connection's current state is closed.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more
information about the error and where it originated in the code.

Exception Details: System.InvalidOperationException: ExecuteNonQuery requires an open and available Connection. The connection's current state is closed.

Source Error:

Line 31: command.ExecuteNonQuery();

I have tried to look at some other threads but none have helped:

ExecuteNonQuery requires an open and available Connection. The connection's current state is closed

MS Access DB doesnt save changes after execution (C#)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

烏雲後面有陽光 2024-12-26 12:28:25

您似乎忘记打开连接(并且为命令分配了错误的连接)。尝试:

       using(OleDbConnection conn = OpenDbConnection())
        {
          using(OleDbCommand command = new OleDbCommand( 
               "INSERT INTO tblUser (UserName, LoginName, Password) VALUES (@name,@login,@pwd)")) 
          {
          command.CommandType = CommandType.Text;
          command.Parameters.Add("@name", OleDbType.VarChar).Value = name; 
          command.Parameters.Add("@login", OleDbType.VarChar).Value = loginName; 
          command.Parameters.Add("@pwd", OleDbType.VarChar).Value = password; 
          command.Connection = conn; 

          conn.Open();

          command.ExecuteNonQuery();
          } 
        }

相反。我也建议使用 using 语句。它将为您管理您的连接。关闭。

You appear to be forgetting to open your connection (and are assigning the wrong connection to the command). Try:

       using(OleDbConnection conn = OpenDbConnection())
        {
          using(OleDbCommand command = new OleDbCommand( 
               "INSERT INTO tblUser (UserName, LoginName, Password) VALUES (@name,@login,@pwd)")) 
          {
          command.CommandType = CommandType.Text;
          command.Parameters.Add("@name", OleDbType.VarChar).Value = name; 
          command.Parameters.Add("@login", OleDbType.VarChar).Value = loginName; 
          command.Parameters.Add("@pwd", OleDbType.VarChar).Value = password; 
          command.Connection = conn; 

          conn.Open();

          command.ExecuteNonQuery();
          } 
        }

Instead. I'd recommend using the using statement, too. It will manage your connection.close for you.

拥有 2024-12-26 12:28:25

OleDbConnection conn = OpenDbConnection();

添加

conn.Open();

或者修改OpenDbConnection如下:

    private OleDbConnection OpenDbConnection()
    {
        string connectionString = GetConnectionString();

        OleDbConnection conn = new OleDbConnection {ConnectionString = connectionString};

        conn.Open();

        return conn;
    }

After

OleDbConnection conn = OpenDbConnection();

add

conn.Open();

Alternatively, modify OpenDbConnection as follows:

    private OleDbConnection OpenDbConnection()
    {
        string connectionString = GetConnectionString();

        OleDbConnection conn = new OleDbConnection {ConnectionString = connectionString};

        conn.Open();

        return conn;
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文