C# - 的ExecuteNonQuery需要一个开放和可用的连接。连接的当前状态为已关闭状态、ExecuteNonQuery

由网友(瞎子撞南墙)分享简介:是新来的C#。请帮助!我不断有以下错误:的ExecuteNonQuery需要一个开放和可用的连接连接的当前状态为已关闭。我不能够插入到我的数据库中。下面是我的code:使用系统;使用System.Collections.Generic;使用System.ComponentModel;使用System.Data...

是新来的C#。 请帮助!

我不断有以下错误:的ExecuteNonQuery需要一个开放和可用的连接连接的当前状态为已关闭。 我不能够插入到我的数据库中。

下面是我的code:

 使用系统;
使用System.Collections.Generic;
使用System.ComponentModel;
使用System.Data这;
使用System.Drawing中;
使用System.Linq的;
使用System.Text;
使用System.Windows.Forms的;
使用System.Data.OleDb;

命名空间AzureSecureStore
{
    公共部分类客户:表
    {
        OleDbConnection的VCON =新的OleDbConnection(@供应商= Microsoft.ACE.OLEDB.12.0;数据源= C:用户 SB18 文档 Visual Studio 2010的项目 AzureSecureStore  AzureSecureStore  AzcureSecureStore Database.accdb;坚持安全信息=假;);
        // OleDbConnection的VCON =新的OleDbConnection(@供应商= Microsoft.ACE.OLEDB.12.0;数据源= C:用户 SB18 文档 Visual Studio 2010的项目 AzureSecureStore  AzureSecureStore  AzcureSecureStore Database.accdb);
        公共客户端()
        {
            的InitializeComponent();

        }

        私人无效的button1_Click(对象发件人,EventArgs的)
        {

        }

        私人无效dataGridView1_CellContentClick(对象发件人,DataGridViewCellEventArgs E)
        {

        }

        私人无效button5_Click(对象发件人,EventArgs的)
        {

        }

        私人无效button4_Click(对象发件人,EventArgs的)
        {

        }

        私人无效Client_Load(对象发件人,EventArgs的)
        {
            // TODO:这行code加载数据到azcureSecureStore_DatabaseDataSet5.Client'表。您可以移动,或将其删除,因为需要的。
            this.clientTableAdapter.Fill(this.azcureSecureStore_DatabaseDataSet5.Client);
            // TODO:这行code加载数据到azcureSecureStore_DatabaseDataSet2.Client'表。您可以移动,或将其删除,因为需要的。
            //this.clientTableAdapter.Fill(this.azcureSecureStore_DatabaseDataSet2.Client);

        }

        私人无效button2_Click(对象发件人,EventArgs的)
        {
            弦AB =的String.Format(插入到客户端的值({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 =新的OleDbCommand(AB,VCON);
            vcom.ExecuteNonQuery();
            的MessageBox.show(数据存储成功);
            vcom.Dispose();
        }

        私人无效textBox1_TextChanged(对象发件人,EventArgs的)
        {

        }
    }
}
 

解决方案

您忘了打开连接,

  vcon.Open();
vcom.ExecuteNonQuery();
 
C 内存管理

但记得要使用

使用语句 - 正确处理对象 的try-catch 块 - 正确捕捉异常(异常处理的)

更新1

 字符串connStr = @供应商= Microsoft.ACE.OLEDB.12.0;数据源= C:用户 SB18 文档 Visual Studio 2010的项目 AzureSecureStore  AzureSecureStore  AzcureSecureStore Database.accdb;持续安全信息= FALSE;;
查询字符串=INSERT INTO客户VALUES(@ COL1,COL2 @,@ COL3,@ COL4,@ COL5,@ COL6);
使用(OleDbConnection的康恩=新的OleDbConnection(connStr))
{
    使用(OleDbCommand的通讯=新的OleDbCommand())
    {
        comm.Connection =康涅狄格州;
        comm.CommandText =查询;
        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));
        尝试
        {
            conn.Open();
            comm.ExecuteNonQuery();
            的MessageBox.show(数据存储成功);
        }
        赶上(OleDbException E)
        {
            的MessageBox.show(e.ToString());
        }
    }
}
 

AddWithValue 添加(推荐使用的)

am new to C#. Please assist!

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.

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:UsersSB18DocumentsVisual Studio 2010ProjectsAzureSecureStoreAzureSecureStoreAzcureSecureStore Database.accdb; Persist Security Info=False;");
        //OleDbConnection vcon = new OleDbConnection(@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:UsersSB18DocumentsVisual Studio 2010ProjectsAzureSecureStoreAzureSecureStoreAzcureSecureStore 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)
        {

        }
    }
}

解决方案

you forgot to open the connection,

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

but remember to use

using statement -- to properly dispose objects try-catch block -- to properly catch exceptions (exception handling)

UPDATE 1

string connStr = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:UsersSB18DocumentsVisual Studio 2010ProjectsAzureSecureStoreAzureSecureStoreAzcureSecureStore 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());
        }
    }
}

AddWithValue Add (recommended to use)

阅读全文

相关推荐

最新文章