我在另一个数据阅读器中打开一个数据阅读器,但是当我打开第二个数据阅读器并尝试读取数据时,它表示即使我刚打开它也会关闭它。
这是我的功能:
protected void checkBookStockAgain()
{
SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionStore"].ConnectionString);
conn.Open();
string selectCartString = "Select Item, Price, Quantity From ShoppingCart Where Username = @User";
SqlCommand cmd = new SqlCommand(selectCartString, conn);
cmd.Parameters.AddWithValue("@User", Session["user"].ToString());
SqlDataReader readCart = cmd.ExecuteReader();
while (readCart.Read())
{
string selectBookInfo = "Select Quantity from BookInfo where Title = @Title";
SqlCommand bookInfoQuantitycmd = new SqlCommand(selectBookInfo, conn);
bookInfoQuantitycmd.Parameters.AddWithValue("@Title", readCart["Item"]);
SqlDataReader quantityReader = bookInfoQuantitycmd.ExecuteReader();
while (quantityReader.Read())
{
if (Convert.ToInt32(readCart["Quantity"]) > Convert.ToInt32(quantityReader["Quantity"]))
{
updateCart = false;
quantityReader.Close();
readCart.Close();
conn.Close();
}
else if (Convert.ToInt32(readCart["Quantity"]) < Convert.ToInt32(quantityReader["Quantity"]))
{
updateCart = true;
invQuantity = Convert.ToInt32(quantityReader["Quantity"].ToString()) - Convert.ToInt32(readCart["Quantity"].ToString());
quantityReader.Close();
readCart.Close();
conn.Close();
}
else if (Convert.ToInt32(readCart["Quantity"]) == Convert.ToInt32(quantityReader["Quantity"]))
{
updateCart = true;
removeBookFromStore(readCart);
quantityReader.Close();
readCart.Close();
conn.Close();
}
else
{
quantityReader.Close();
readCart.Close();
conn.Close();
}
}
}
}
给出的错误指出“读取器关闭时无效尝试调用读取”,并且尝试通过quantityReader运行while循环时发生错误。作为旁注,我在连接字符串中启用了MARS(多个活动结果集)。