我需要循环访问表格中的所有记录

时间:2014-04-27 11:41:02

标签: asp.net sql-server database sql-server-2008

我需要循环访问我的表中的所有记录,因为这在我的项目“拍卖网络系统”中无法正常工作,我在这里使用Web服务来定期检查产品的状态,以及何时打开状态和数据到目前为止,更新产品并将其状态设置为“已关闭”。这里的代码只适用于当时的一行!我需要同时检查所有行。

    { string sql12 = "SELECT item_id FROM items Where status='opened' AND endDate<=@endate ";

    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();
   }

2 个答案:

答案 0 :(得分:0)

将结果导入datareader MSFT DOCU 然后将剩下的代码包装在

while(reader.Read()) 
{
    REST OF CODE 
}

这个问题也可以帮到你。 Reader Question

答案 1 :(得分:0)

您可以通过此查询直接更新您的表格

UPDATE项目SET status ='closed'WHERE item_id in(SELECT item_id FROM items where status ='opened'AND endDate&lt; = @ endate)