无法重用打开的DataReader

时间:2015-01-04 13:26:58

标签: c# sqldatareader

我正在编写一个c#windows窗体应用程序,我正在解决上面提到的错误。我认为这是发生的,因为我在我的主窗体加载对象中打开一个sql连接和读取器对象,然后在另一个单击事件处理程序中再次执行相同的操作。我不知道我需要做些什么来改变我的代码/阻止这种情况发生(或者如果这甚至是问题)。我已尝试在我的连接字符串中打开MARS,但这并没有解决问题。以下是我的代码。

   namespace Excel_Importer
    {
public partial class Export : Form
{
    public Export()
    {
        InitializeComponent();
    }

    private void cmbItemLookup_SelectedIndexChanged(object sender, EventArgs e)
    {

    }

    private void Export_Load(object sender, EventArgs e)
    {
                                                                                           string connectionString = ConfigurationManager.ConnectionStrings ["dbconnection"].ConnectionString;

        using (SqlConnection conn = new SqlConnection(connectionString))
        {
            conn.Open();

            SqlCommand cmd = new SqlCommand("Select * From ExportItem", conn);
            SqlDataReader rdr;
            rdr = cmd.ExecuteReader();

            System.Data.DataTable dt = new System.Data.DataTable();
            dt.Columns.Add("ExportItemName", typeof(string));
            dt.Load(rdr);

            cmbItemLookup.DisplayMember = "ExportItemName";
            cmbItemLookup.DataSource = dt;
            conn.Close();
        }
    }

    private void btnLoad_Click(object sender, EventArgs e)
    {
        string connectionString = ConfigurationManager.ConnectionStrings["dbconnection"].ConnectionString;

        SqlConnection conn = new SqlConnection(connectionString);
        conn.Open();
        SqlCommand cmd2 = new SqlCommand("Select * from " + cmbItemLookup.Text, conn);
        SqlDataReader rdr2;
        rdr2 = cmd2.ExecuteReader();

        try
        {

            SqlDataAdapter ada = new SqlDataAdapter();
            ada.SelectCommand = cmd2;
            DataTable dt = new DataTable();
            ada.Fill(dt);
            BindingSource bs = new BindingSource();
            bs.DataSource = dt;
            dgvExport.DataSource = bs;
            ada.Update(dt);
            conn.Close();
        }

        catch (Exception Ex)
        {
            MessageBox.Show(Ex.Message);
        }

    }


}

}

2 个答案:

答案 0 :(得分:2)

您需要关闭DataReaders。它们实现了IDisposable接口,因此最简单的方法是将它们放在using块中:

using (rdr = cmd.ExecuteReader())
{
  ..
} // .NET always calls Dispose() for you here

实际上,你几乎必须处理实现IDisposable的所有事情,否则会发生问题。

答案 1 :(得分:0)

正如其他答案所指出的那样,您必须整理点击的事件代码:

private void btnLoad_Click(object sender, EventArgs e)
{
    string connectionString = ConfigurationManager.ConnectionStrings["dbconnection"].ConnectionString;

    using (SqlConnection conn = new SqlConnection(connectionString) )
    {
      conn.Open();
      using(SqlCommand cmd2 = new SqlCommand("Select * from " + cmbItemLookup.Text, conn) )
      {
        using(SqlDataReader rdr2= cmd2.ExecuteReader())
        {

          try
          {

            SqlDataAdapter ada = new SqlDataAdapter();
            ada.SelectCommand = cmd2;
            DataTable dt = new DataTable();
            ada.Fill(dt);
            BindingSource bs = new BindingSource();
            bs.DataSource = dt;
            dgvExport.DataSource = bs;
            ada.Update(dt);
            conn.Close();
          }
          catch (Exception Ex)
          {
            MessageBox.Show(Ex.Message);
          }
        }
      }
    }

}