當試圖在我的DataReader中間執行刪除我不斷收到此錯誤:CLR信息:System.InvalidOperationException:上下文連接已在使用
System.InvalidOperationException:上下文連接已在使用。
我得到所有需要刪除的行,然後遍歷數據讀取器,因爲返回的行很多。
SqlConnection conn = new SqlConnection(「context connection = true」);
SqlCommand cmd = new SqlCommand();
cmd.Connection = conn;
cmd.CommandType = CommandType.StoredProcedure;
cmd.CommandText = "spClean_Select_Product_For_Delete";
cmd.CommandTimeout = 41600;
conn.Open();
SqlDataReader reader = cmd.ExecuteReader();
try
{
SqlContext.Pipe.Send(DateTime.Now.ToString() + " - Started working with Product");
while (reader.Read())
{
SqlContext.Pipe.ExecuteAndSend(new SqlCommand("DELETE FROM ProductInfo WHERE ProductId = "
+ reader["ProductId"].ToString()));
}
SqlContext.Pipe.Send(DateTime.Now.ToString() + " - Completed working with Product");
}
finally
{
conn.Close();
// Always call Close when done reading.
reader.Close();
}
如果以下任何答案是正確的,您能否將其標記爲已回答? – 2011-04-26 05:57:51