C# - ExecuteNonQuery 需要打开且可用的连接。连接的当前状态是关闭的

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14291202/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 11:18:57  来源:igfitidea点击:

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

c#.netsqlms-access

提问by user1971823

am new to C#. Please assist!

我是 C# 的新手。请协助!

I keep having the following error: "ExecuteNonQuery requires an open and available Connection. The connection's current state is closed." I am not able to insert into my database as well.

我一直有以下错误:“ExecuteNonQuery 需要打开且可用的连接。连接的当前状态已关闭。” 我也无法插入到我的数据库中。

Below is my code:

下面是我的代码:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.OleDb;

namespace AzureSecureStore
{
    public partial class Client : Form
    {
        OleDbConnection vcon = new OleDbConnection(@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\SB18\Documents\Visual Studio 2010\Projects\AzureSecureStore\AzureSecureStore\AzcureSecureStore Database.accdb; Persist Security Info=False;");
        //OleDbConnection vcon = new OleDbConnection(@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\SB18\Documents\Visual Studio 2010\Projects\AzureSecureStore\AzureSecureStore\AzcureSecureStore Database.accdb");
        public Client()
        {
            InitializeComponent();

        }

        private void button1_Click(object sender, EventArgs e)
        {

        }

        private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {

        }

        private void button5_Click(object sender, EventArgs e)
        {

        }

        private void button4_Click(object sender, EventArgs e)
        {

        }

        private void Client_Load(object sender, EventArgs e)
        {
            // TODO: This line of code loads data into the 'azcureSecureStore_DatabaseDataSet5.Client' table. You can move, or remove it, as needed.
            this.clientTableAdapter.Fill(this.azcureSecureStore_DatabaseDataSet5.Client);
            // TODO: This line of code loads data into the 'azcureSecureStore_DatabaseDataSet2.Client' table. You can move, or remove it, as needed.
            //this.clientTableAdapter.Fill(this.azcureSecureStore_DatabaseDataSet2.Client);

        }

        private void button2_Click(object sender, EventArgs e)
        {
            string ab = string.Format("insert into Client values({0}, '{1}', '{2}', {3}, {4}, '{5}')",
    textBox1.Text, textBox2.Text, int.Parse(textBox3.Text), int.Parse(textBox4.Text), textBox9.Text, int.Parse(textBox10.Text));
            OleDbCommand vcom = new OleDbCommand(ab, vcon);
            vcom.ExecuteNonQuery();
            MessageBox.Show("Data stored successfully");
            vcom.Dispose();
        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {

        }
    }
}

回答by John Woo

you forgot to open the connection,

你忘了打开连接,

vcon.Open();
vcom.ExecuteNonQuery();

but remember to use

但记得使用

  • usingstatement -- to properly dispose objects
  • try-catchblock -- to properly catch exceptions (exception handling)
  • using声明——正确处置对象
  • try-catch块 - 正确捕获异常(异常处理

UPDATE 1

更新 1

string connStr = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\SB18\Documents\Visual Studio 2010\Projects\AzureSecureStore\AzureSecureStore\AzcureSecureStore Database.accdb; Persist Security Info=False;";
string query = "INSERT INTO Client VALUES(@col1,@col2,@col3,@col4,@col5,@col6)";
using (OleDbConnection conn = new OleDbConnection(connStr))
{
    using (OleDbCommand comm = new OleDbCommand())
    {
        comm.Connection = conn;
        comm.CommandText = query;
        comm.CommandType = CommandType.Text;
        comm.Parameters.AddWithValue("@col1", textBox1.Text);
        comm.Parameters.AddWithValue("@col2", textBox2.Text);
        comm.Parameters.AddWithValue("@col3", int.Parse(textBox3.Text));
        comm.Parameters.AddWithValue("@col4", int.Parse(textBox4.Text));
        comm.Parameters.AddWithValue("@col5", textBox9.Text);
        comm.Parameters.AddWithValue("@col6", int.Parse(textBox10.Text));
        try
        {
            conn.Open();
            comm.ExecuteNonQuery();
            MessageBox.Show("Data stored successfully");
        }
        catch(OleDbException e)
        {
            MessageBox.Show(e.ToString());
        }
    }
}

回答by Harshit Tailor

For connect with database you need to open connection so use

为了连接数据库,你需要打开连接所以使用

vcon.Open();

and then

进而

vcom.ExecuteNonQuery();