在两个DataReader之间切换

时间:2018-05-01 10:15:40

标签: c# sql ado.net

我有以下代码:

private void supplBox_SelectedIndexChanged(object sender, EventArgs e)
{
   if (sqlcon.State == ConnectionState.Closed)
   {
      sqlcon.Open();
   }
   cmd = new SqlCommand("Select * from Supplier_info where Supplier_name = '" + supplBox.Text + "'", sqlcon);
   cmd.ExecuteNonQuery();
   SqlDataReader reader = cmd.ExecuteReader();
   while (reader.Read())
   {
      suppNum.Text = Convert.ToString(reader.GetInt32(0));
   }
   reader.Close();
   sqlcon.Close();
   barCode1.Focus();
}
private void suppNum_KeyUp(object sender, KeyEventArgs e)
{
   if (sqlcon.State == ConnectionState.Closed)
   {
      sqlcon.Open();
   }
   cmd = new SqlCommand("Select * from Supplier_info where Supplier_id = '" + suppNum.Text + "'", sqlcon);
   cmd.ExecuteNonQuery();
   SqlDataReader reader = cmd.ExecuteReader();
   while (reader.Read())
   {
      supplBox.Text = reader.GetString(1);
   }
   reader.Close();
   sqlcon.Close();
   barCode1.Focus();
}

当我在supplBox和suppNum之间切换时,我收到此错误:

  

已经有一个与此命令关联的打开DataReader,必须先关闭它。

如果我将MultipleActiveResultSets=true添加到我的连接中,则会出现此错误:

  

读取器关闭时无效尝试调用Read。

请帮助

0 个答案:

没有答案