Question

I need to loop for all records in my table , cose here is not working properly in my project "Auction web system" , I use web service here to check the status of product Periodically , and when the status is opened and data time is less to now , update the product and set its status to "closed". the code here work only for one row at the time ! I need to check for all rows at the same time. { 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();
       }
Était-ce utile?

La solution

You can just use one update statement to update all the items status to 'colesed' where the enddate is passed. This is only if you don't need the item_id somewhere else.

So your code above can be;

    string sql123 = "UPDATE items SET status ='closed' Where status='opened' AND endDate<=GETDATE()";
    SqlCommand cmd21 = new SqlCommand(sql123, con);
    cmd21.ExecuteNonQuery();
    con.Close();

Autres conseils

You can combine both queries into one query and update directly as follows:

Update items
Set status = 'closed'
Where status='opened' 
AND endDate<=@endate

Then pass the required value as parameter and run the query with ExecuteNonQuery method.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top