2013-06-24 50 views
1

我有下面的代码和我得到异常已经没有与此命令相关联的打开的DataReader,必须首先在C#中被关闭

“已经有与此连接 相关联的打开的DataReader必须是首先关闭“。

我对此项目使用Microsoft Visual C#2010 Express和Microsoft Access 2007。

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

}}

+0

检查此问题http://stackoverflow.com/questions/5440168/c-sharp-mysql-there-is-already-an-open-datareader-associated-with-this-connectio –

+0

您可以调用ExecuteReader两次Button_Click事件。而下一个'label2.Text = command.ExecuteReader()。ToString()'没有意义 – Steve

回答

0

您忘记关闭的读者对形式的构造器。当您执行button1_Click读取器已经打开时

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

表单的构造函数中的读取器未关闭。你应该考虑使用,以构建工作,以避免这种情况:

using (var reader = command.ExecuteReader(CommandBehavior.CloseConnection)) 
{ 
    ... 
} 
+0

+1比我的解决方案更好。 :) –

+0

Thankz的回复,但实际上我是新来的这个。我应该在哪里使用这个“使用构造”? – Lany

+0

用'using'构造替换读者的声明和赋值,并用大括号将阅读部分(即循环)括起来,这样'使用'块对该部分是活动的。也可以考虑阅读有关MSDN在线的“使用”。顺便说一句:我也很新。 – alzaimar

0

Retrieving Data Using a DataReader

注意,虽然一个DataReader是开放的,连接在使用 完全由DataReader的。您无法执行连接的任何命令 ,包括创建另一个DataReader,,直到 原始DataReader关闭。

要么你应该关闭当前OleDbDataReader或者每个OleDbDataReader定义不同OleDbConnection

+0

先生,我已经尝试过,但没有得到那个。 Plzz帮助代码。 – Lany

相关问题