1

以下のコードがあり、例外が発生しています

「この接続に関連付けられた開いている 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();
        }          
    }

} }

4

3 に答える 3

4

Form のコンストラクターのリーダーが閉じられていません。これを回避するには、using コンストラクトを使用することを検討する必要があります。

using (var reader = command.ExecuteReader(CommandBehavior.CloseConnection))
{
   ... 
}
于 2013-06-24T06:06:20.587 に答える
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();
}
于 2013-06-24T06:05:41.590 に答える
0

からRetrieving Data Using a DataReader

DataReader が開いている間、Connection はその DataReader によって排他的に使用されることに注意してください。元の DataReader が閉じられるまで、別の DataReader の作成を含め、接続に対してコマンドを実行することはできません。

現在のを閉じるか、それぞれにOleDbDataReader異なる定義をする必要があります。OleDbConnectionOleDbDataReader

于 2013-06-24T06:07:34.020 に答える