我需要循环访问我的表中的所有记录,因为这在我的项目中没有正常工作"拍卖网络系统" ,我在这里使用Web服务定期检查产品状态,当状态打开且数据时间减少到现在时,更新 产品并将其状态设置为"已关闭"。 这里的代码只适用于当时的一行! 我需要同时检查所有行。 { string sql12 =" SELECT item_id FROM items where status =' open' AND endDate< = @ endate&#34 ;;
SqlCommand cmd12 = new SqlCommand(sql12, con);
con.Open();
cmd12.Parameters.AddWithValue("@endate", DateTime.Now);
query = Convert.ToInt32(cmd12.ExecuteScalar());
string sql123 = "UPDATE items SET status ='closed' WHERE item_id =@Item_ID";
SqlCommand cmd21 = new SqlCommand(sql123, con);
cmd21.Parameters.AddWithValue("@Item_ID", query);
cmd21.ExecuteNonQuery();
con.Close();
CalculateWinningPrice(query);
}
public void CalculateWinningPrice(Int32 query)
{
string sql1 = "SELECT MAX(Bid_price) AS Expr1 FROM Bid WHERE (item_id = @Item_ID)";
SqlCommand cmd1 = new SqlCommand(sql1, con);
con.Open();
cmd1.Parameters.AddWithValue("@Item_ID", query);
Int32 max = Convert.ToInt32(cmd1.ExecuteScalar());
SqlCommand cmd3 = new SqlCommand("SELECT user_id FROM Bid WHERE(Bid_price =(SELECT MAX(Bid_price) AS Expr1 FROM Bid AS BID_1 WHERE(item_id = @Item_ID)))", con);
cmd3.Parameters.AddWithValue("@Item_ID", query);
Int32 winner = Convert.ToInt32(cmd3.ExecuteScalar());
SqlCommand cmd4 = new SqlCommand("SELECT name FROM items WHERE (item_id=@Item_ID)",con);
cmd4.Parameters.AddWithValue("Item_ID", query);
string product_name = Convert.ToString(cmd4.ExecuteScalar());
GeneratePDF.create_pdf(product_name, Convert.ToDecimal(max).ToString("c"), DateTime.Now.ToString());
SqlCommand cmd = new SqlCommand("INSERT INTO Winners VALUES(@item_id, @user_id,@win_price,@win_date)");
cmd.CommandType = CommandType.Text;
cmd.Parameters.AddWithValue("@item_id", query);
cmd.Parameters.AddWithValue("@user_id", winner);
cmd.Parameters.AddWithValue("@win_price", max);
cmd.Parameters.AddWithValue("@win_date", DateTime.Now);
cmd.Connection = con;
cmd.ExecuteNonQuery();
con.Close();
}
答案 0 :(得分:2)
您可以使用一个更新语句将所有项目状态更新为传递enddate的'colesed'。只有在其他地方不需要item_id时才会这样。
所以上面的代码可以是;
string sql123 = "UPDATE items SET status ='closed' Where status='opened' AND endDate<=GETDATE()";
SqlCommand cmd21 = new SqlCommand(sql123, con);
cmd21.ExecuteNonQuery();
con.Close();
答案 1 :(得分:0)
您可以将两个查询合并为一个查询并直接更新,如下所示:
Update items
Set status = 'closed'
Where status='opened'
AND endDate<=@endate
然后将所需的值作为参数传递,并使用ExecuteNonQuery
方法运行查询。