微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

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

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

以下是我的代码

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,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)
        {

        }
    }
}

解决方法

你忘了打开连接,
vcon.open();
vcom.ExecuteNonQuery();

但记得要用

> using statement – 正确处理对象
> try-catch块 – 正确捕获异常(异常处理)

更新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());
        }
    }
}

> AddWithValue
> Add (recommended to use)

原文地址:https://www.jb51.cc/csharp/91197.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐