C# 已经有一个与此命令关联的打开的DataReader,在C中必须首先关闭该命令#
我有下面的代码,我得到异常 “已存在与此连接关联的打开的DataReader 必须先关上它” 我正在使用Microsoft Visual C#2010 Express和Microsoft Access 2007进行此项目C# 已经有一个与此命令关联的打开的DataReader,在C中必须首先关闭该命令#,c#,ms-access-2007,C#,Ms Access 2007,我有下面的代码,我得到异常 “已存在与此连接关联的打开的DataReader 必须先关上它” 我正在使用Microsoft Visual C#2010 Express和Microsoft Access 2007进行此项目 namespace Database1 { public partial class Form1 : Form { OleDbConnection connection; public void connect()
namespace Database1
{
public partial class Form1 : Form
{
OleDbConnection connection;
public void connect()
{
connection = new OleDbConnection(@"Provider=Microsoft.ACE.OLEDB.12.0;DataSource=|DataDirectory|\PBName1.accdb;Data Source=C:\Users\bvino_000\Downloads\PBName1.accdb");
connection.Open();
}
public void close_connection()
{
connection.Close();
}
public Form1()
{
InitializeComponent();
connect();
OleDbDataReader reader = null;
OleDbCommand command = new OleDbCommand("SELECT * from PBInfo", connection);
reader = command.ExecuteReader();
while (reader.Read())
{
listBox1.Items.Add(reader[1].ToString());
}
close_connection();
}
private void button1_Click(object sender, EventArgs e)
{
listBox2.Items.Add(listBox1.SelectedItem);
string s = "";
s = listBox1.SelectedItem.ToString();
connect();
string sql = "SELECT PBSize FROM PBInfo where PBName=" + " '" + s + "' ";
try
{
OleDbDataReader reader = null;
OleDbCommand command = new OleDbCommand(sql, connection);
reader = command.ExecuteReader(CommandBehavior.CloseConnection);
while (reader.Read())
{
command.ExecuteReader();
}
reader.Close();
command.Dispose();
close_connection();
if (connection.State != ConnectionState.Open)
{
connection.Open();
}
label2.Text = command.ExecuteReader().ToString();
listBox1.Items.Remove(listBox1.SelectedItem);
}
catch(Exception ex)
{
ex.GetBaseException();
}
finally
{
close_connection();
}
}
}
}您忘记关闭form Constructor上的阅读器。当您执行按钮1时,单击一个读卡器已准备好打开
public Form1()
{
InitializeComponent();
connect();
OleDbDataReader reader = null;
OleDbCommand command = new OleDbCommand("SELECT * from PBInfo", connection);
reader = command.ExecuteReader();
while (reader.Read())
{
listBox1.Items.Add(reader[1].ToString());
}
**reader.Close();
command.Dispose();**
//close_connection();
}
您忘记关闭窗体构造函数上的阅读器。当您执行按钮1时,单击一个读卡器已准备好打开
public Form1()
{
InitializeComponent();
connect();
OleDbDataReader reader = null;
OleDbCommand command = new OleDbCommand("SELECT * from PBInfo", connection);
reader = command.ExecuteReader();
while (reader.Read())
{
listBox1.Items.Add(reader[1].ToString());
}
**reader.Close();
command.Dispose();**
//close_connection();
}
窗体构造函数中的读取器未关闭。你应该考虑使用这个构造来避免这个:
using (var reader = command.ExecuteReader(CommandBehavior.CloseConnection))
{
...
}
窗体构造函数中的读取器未关闭。你应该考虑使用这个构造来避免这个:
using (var reader = command.ExecuteReader(CommandBehavior.CloseConnection))
{
...
}
从
请注意,当DataReader打开时,连接正在使用中
由那个数据阅读器独家提供。您不能对执行任何命令
连接,包括创建另一个DataReader,,直到
原始数据读取器已关闭。
您应该关闭当前或为每个选项定义不同的选项。从
请注意,当DataReader打开时,连接正在使用中
由那个数据阅读器独家提供。您不能对执行任何命令
连接,包括创建另一个DataReader,,直到
原始数据读取器已关闭。
您应该关闭当前按钮或为每个按钮定义不同的按钮。在按钮单击事件中调用ExecuteReader两次时,请选中此项。下一个
label2.Text=command.ExecuteReader().ToString()
没有意义在按钮单击事件中选中此选项,然后调用ExecuteReader两次。下一个label2.Text=command.ExecuteReader().ToString()
对回复没有任何意义,但实际上我对这方面还不熟悉。我应该在哪里使用这个“使用构造”?用using
构造替换读卡器的声明和赋值,并用大括号括住读取部分(即循环),以便该部分的using
块处于活动状态。也可以考虑在MSDN网上阅读<<代码>使用<代码>。顺便说一句:我也是新来的。先生,我已经尝试了很多,但是没有办法消除错误。谢谢你的回复,但事实上我是新来的。我应该在哪里使用这个“使用构造”?用using
构造替换读卡器的声明和赋值,并用大括号括住读取部分(即循环),以便该部分的using
块处于活动状态。也可以考虑在MSDN网上阅读<<代码>使用<代码>。顺便说一句:我也是新来的。先生,我试了很多次,但是没有办法消除错误。先生,我试过了,但是没有。先生,我已经试过了,但是没有得到。Plzz代码帮助。