我收到此错误:
已经有一个与此命令关联的打开DataReader,必须先关闭它。
我不知道问题出在哪里。它已关闭,但仍然表示它已经开放。请帮忙!
第一个命令是让所有在两个日期之间休假的员工。
第二个命令我用它来按ID检索日期。
这是我的代码:
using (SqlConnection con = new SqlConnection(connection))
{
con.Open();
SqlCommand cmd = new SqlCommand(" SELECT distinct E.EmployeeId, E.FirstName FROM Employee E INNER JOIN Vacation V ON E.EmployeeId = V.EmployeeId " +
" WHERE ((V.Dates >= @Start AND V.Dates <= @End) ) ", con);
cmd.Parameters.AddWithValue("@Start", (Calendar1.SelectedDates[0]).Date.ToShortDateString());
cmd.Parameters.AddWithValue("@End", (Calendar1.SelectedDates[Calendar1.SelectedDates.Count - 1]).Date.ToShortDateString());
using (SqlDataReader dr = cmd.ExecuteReader())
{
while (dr.Read())
{
Response.Write((dr[1]).ToString() + " "); // Check if retrieves employee name
// Now by Id I want to get all dates belong to specific employee
SqlCommand cmd2 = new SqlCommand("SELECT V.Dates FROM Vacation V " +
" WHERE ((V.Dates >= @Start AND V.Dates <= @End) ) ", con);
cmd2.Parameters.AddWithValue("@Start", (Calendar1.SelectedDates[0]).Date.ToShortDateString());
cmd2.Parameters.AddWithValue("@End", (Calendar1.SelectedDates[Calendar1.SelectedDates.Count - 1]).Date.ToShortDateString());
cmd2.Parameters.AddWithValue("@EmployeeId", Convert.ToInt32(dr[0]));
using (SqlDataReader dr2 = cmd2.ExecuteReader())
{
while (dr2.Read())
{
Response.Write(Convert.ToDateTime(dr2[0]));
}
}
Response.Write("<br/>");
}
GridView7.DataSource = cmd.ExecuteReader();
GridView7.DataBind();
}
con.close();
}
答案 0 :(得分:1)
将此添加到您的连接字符串:
MultipleActiveResultSets=True