我收到一个错误
已经有一个与此命令关联的打开的 DataReader,必须先关闭它。
当我SqlDataReader
在另一个中使用 a 时SqlDataReader
。我不知道我在哪里犯了错误,如何解决?执行第二个数据读取器 (dr2) 时出现错误。
{
cmd = new SqlCommand("select agent,comm,primm from comm where a_id = '" + textBox1.Text + "' AND date >= @date1 AND date <= @date2 ", agr);
cmd.Parameters.AddWithValue("@date1", dateTimePicker1.Value);
cmd.Parameters.AddWithValue("@date2", dateTimePicker2.Value);
dr = cmd.ExecuteReader();
while (dr.Read())
{
aid = dr[0].ToString().Trim();
comm = double.Parse(dr[1].ToString());
busi = double.Parse(dr[2].ToString());
cmd2 = new SqlCommand("select s_id,lvl from agnt where a_id = @a_id", agr);
cmd2.Parameters.AddWithValue("@a_id", aid);
// on this line get executed I get a message
// There is already an open DataReader associated with this
// Command which must be closed first.
dr2 = cmd2.ExecuteReader();
if(dr2.Read())
{
abc = dr2[0].Tostring();
}
dr2.Close();
}
dr.Close();
cmd.Parameters.Clear();
}