C#-ExecuteOnQuery需要一个开放的可用连接。连接';当前状态为关闭
我是C#的新手。 请协助 我一直有以下错误:“ExecuteOnQuery需要一个打开且可用的连接。该连接的当前状态为关闭。” 我也无法插入到我的数据库中 下面是我的代码:C#-ExecuteOnQuery需要一个开放的可用连接。连接';当前状态为关闭,c#,.net,sql,ms-access,C#,.net,Sql,Ms Access,我是C#的新手。 请协助 我一直有以下错误:“ExecuteOnQuery需要一个打开且可用的连接。该连接的当前状态为关闭。” 我也无法插入到我的数据库中 下面是我的代码: using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using S
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)
{
}
}
}
你忘了打开连接
vcon.Open();
vcom.ExecuteNonQuery();
但请记住使用
语句--正确处理对象使用
block——正确捕获异常(异常处理)try catch
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());
}
}
}
vcom.ExecuteNonQuery();
您好,我做了您提到的,但是我得到了以下错误:“OLEDBEException未处理。”没有为一个或多个参数提供值。我应该怎么做?嗯,我做了更改。但是数据仍然无法进入我的数据库。这不完全是一个错误,但当我单击insert时,会弹出以下窗口:“Systems.Windows.Forms.MouseEventArgs”代码可以自行运行。尝试将其放入一个方法中,并调用该方法。@user1971823-听起来您在
catch
子句中调用了ToString()
的变量是错误的-它应该是对上面两行异常的引用。您可能必须更改该引用,因为此答案中的示例使用了e
,但是e
是您函数的第二个参数。您好,我执行了您提到的操作,但出现以下错误:“OLEDBEException未处理”。没有为一个或多个参数提供值。我该怎么办?
vcom.ExecuteNonQuery();