C# - 的ExecuteNonQuery需要一个开放和可用的连接。连接的当前状态为已关闭 [英] C# - ExecuteNonQuery requires an open and available Connection. The connection's current state is closed
本文介绍了C# - 的ExecuteNonQuery需要一个开放和可用的连接。连接的当前状态为已关闭的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
是新来的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();
但记得要使用
-
使用
语句 - 正确处理对象 -
的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());
}
}
}
- <一个href="http://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqlparametercollection.addwithvalue.aspx"相对=nofollow> 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:\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)
{
}
}
}
解决方案
you forgot to open the connection,
vcon.Open();
vcom.ExecuteNonQuery();
but remember to use
using
statement -- to properly dispose objectstry-catch
block -- to properly catch exceptions (exception handling)
UPDATE 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());
}
}
}
这篇关于C# - 的ExecuteNonQuery需要一个开放和可用的连接。连接的当前状态为已关闭的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文