获取2个日期之间的价格

时间:2017-12-05 03:09:06

标签: c# sql

我试图根据酒店的关税得到两个日期之间的价格。

enter image description here

var dates = new List<DateTime>();
for (var dt = checkIn; dt <= checkOut; dt = dt.AddDays(1))
{
    dates.Add(dt);
}

try
{
    cnn = new MySqlConnection(connString);
    cnn.ConnectionString = connString;
    cnn.Open();

    for (int i = 0; i < dates.Count - 1; i++)
    {
        lblResult.Text = "Pass";
        string da = dates[i].Year + "-" + dates[i].Month + "-" + dates[i].Day;
        DayOfWeek da1 = dates[i].DayOfWeek;
        if (dates[i].Day == 1 && dates[i].Month == 1 || dates[i].Day == 9 && dates[i].Month == 1 || dates[i].Day == 11 && dates[i].Month == 2 || dates[i].Day == 20 && dates[i].Month == 3 || dates[i].Day == 29 && dates[i].Month == 4 || dates[i].Day == 3 && dates[i].Month == 5 || dates[i].Day == 4 && dates[i].Month == 5 || dates[i].Day == 5 && dates[i].Month == 5 || dates[i].Day == 17 && dates[i].Month == 7 || dates[i].Day == 11 && dates[i].Month == 8 || dates[i].Day == 18 && dates[i].Month == 9 || dates[i].Day == 22 && dates[i].Month == 9 || dates[i].Day == 9 && dates[i].Month == 10 || dates[i].Day == 3 && dates[i].Month == 11 || dates[i].Day == 23 && dates[i].Month == 11 || dates[i].Day == 23 && dates[i].Month == 12)
        {
            sql = "SELECT GrossPrice FROM HotelPriceFIT WHERE HotelName='" + hotelName + "' AND RmType='" + rmType + "' AND Remarks LIKE '%Eve of Japan Holiday%' AND DATE(StartDate) >='" + da + "' AND DATE(EndDate) <='" + da + "'";
            ClientScript.RegisterStartupScript(GetType(), "alert", "alert('Pass 2');", true);
        }
        else if (da1.ToString() == "Saturday")
        {
            sql = "SELECT GrossPrice FROM HotelPriceFIT WHERE HotelName='" + hotelName + "' AND RmType='" + rmType + "' AND Remarks LIKE '%Sat%' AND DATE(StartDate) >='" + da + "' AND DATE(EndDate) <='" + da + "'";
            ClientScript.RegisterStartupScript(GetType(), "alert", "alert('Pass 3');", true);
        }
        else
        {
            **sql = "SELECT GrossPrice FROM HotelPriceFIT WHERE HotelName='" + hotelName + "' AND RmType='" + rmType + "' AND DATE(StartDate) >='" + da + "' AND DATE(EndDate) <='" + da + "'";**
            ClientScript.RegisterStartupScript(GetType(), "alert", "alert('Pass 4');", true);
            lblPassHotel.Text = sql;
        }
        cmd = new MySqlCommand(sql, cnn);
        reader = cmd.ExecuteReader();
        if (reader.Read() == true)
        {
            amount = amount + reader.GetInt16(0);
        }
        reader.Close();
    }
    cnn.Close();
}

如果我选择

2018-01-18作为开始日期,2018-01-19作为结束日期,它应该给我9000。

感谢任何形式的支持。

空集

1 个答案:

答案 0 :(得分:1)

这里有多个问题:

  1. 您需要使用()括号对此行进行分组。由于你没有分组,你会让自己感到困惑。将所有&&分组或将所有||分组,以便最终条件仅为&&(或仅||

    if (dates[i].Day == 1 && dates[i].Month == 1 || dates[i].Day == 9 && dates[i].Month == 1 || dates[i].Day == 11 && dates[i].Month == 2 || dates[i].Day == 20 && dates[i].Month == 3 || dates[i].Day == 29 && dates[i].Month == 4 || dates[i].Day == 3 && dates[i].Month == 5 || dates[i].Day == 4 && dates[i].Month == 5 || dates[i].Day == 5 && dates[i].Month == 5 || dates[i].Day == 17 && dates[i].Month == 7 || dates[i].Day == 11 && dates[i].Month == 8 || dates[i].Day == 18 && dates[i].Month == 9 || dates[i].Day == 22 && dates[i].Month == 9 || dates[i].Day == 9 && dates[i].Month == 10 || dates[i].Day == 3 && dates[i].Month == 11 || dates[i].Day == 23 && dates[i].Month == 11 || dates[i].Day == 23 && dates[i].Month == 12)
    
  2. 这就是为什么总是持续执行Else条件。

    1. 此代码容易受到SQL注入攻击。参数化您的查询。