【问题标题】:How to Check if Item is available for certain Range of Date如何检查项目是否适用于特定日期范围
【发布时间】:2016-08-18 17:21:54
【问题描述】:

我正在使用 SQL DB 构建 C# Inventory 应用程序。使用此应用程序,您可以选择要预订的项目,如果可用,它将被预订。听起来很容易啊!,我也是这么想的。这是问题所在。 此应用程序需要检查两个条件。

1) 如果商品在特定日期范围内可用(例如:8 月 18 日至 8 月 23 日)

2) 如果需要的数量可用(例如:2 或 3)。

我有一个包含项目名称和初始数量的数据库。另一个持有当前预订。其中有以下数据。

ID   Cottage    quantity   From Date    To Date     Item name
 2   Woodcastle    2       2016-08-18   2016-08-24   Kayaks

现在如果我选择 1 Kayak(初始数量 3)从 2016 年 8 月 19 日到 2016 年 8 月 23 日预订。如何使用 SQL 做到这一点?

这是我目前正在做的事情,但没有运气

selecteditem = items_listbox.SelectedItem.ToString();
       from_date = from_datepicker.Value.ToString();
       to_date = to_datepicker.Value.ToString();
        quantity = quantity_need.Text;
        from_date = Convert.ToDateTime(from_date).ToString("yyyy-MM-dd");
        to_date = Convert.ToDateTime(to_date).ToString("yyyy-MM-dd");
        int init_i = 0;
        int taken_i = 0;
        int dropped_i = 0;
        string select_init = "SELECT initial_quantity from inventory_items Where item_name LIKE '" + selecteditem + "'; "+ "SELECT COALESCE(SUM(Quantity), 0) from inventory_bookings Where item_name LIKE '" + selecteditem + "' AND '" + from_date + "' between date_from and date_to; "+ "SELECT COALESCE(SUM(Quantity), 0) from inventory_bookings Where item_name LIKE '" + selecteditem + "' AND '" + to_date + "' between date_from and date_to";
        conn = new MySqlConnection( connectionstring);
        MySqlCommand init = new MySqlCommand(select_init, conn);
        try
        {
            conn.Open();
            reader = init.ExecuteReader();
            while (reader.Read())
            {
                init_i = reader.GetInt32(0);
            }
            reader.NextResult();
            while (reader.Read())
            {
               taken_i = reader.GetInt32(0);
            }
            reader.NextResult();
            while (reader.Read())
            {
                dropped_i = reader.GetInt32(0);
            }
            int quantity_avail= init_i - taken_i - dropped_i;
            if (quantity_avail >= Convert.ToInt32(quantity_need.Text))
            {
                checkresult_lbl.Text= "Currently There are "+ quantity_avail + " "+ selecteditem +"/s available in your Inventory, Please Proceed to book";
                Booking_btn.Enabled = true;
            }
           else
            {
                checkresult_lbl.Text = "Currently There are not enough available Items in your Inventory, Please change date or Quantity";
            }
         }
        catch (Exception ex)
        {
            MessageBox.Show(ex.ToString());
        }
        finally
        {
            conn.Close();
        }

【问题讨论】:

  • 附注 - 请使用参数化查询!通过将所选列表框项中的值直接粘贴到查询中,您可能会在此处引入漏洞。阅读一下 SQL 注入。
  • 我总是坚持使用参数化查询。这仅适用于开发阶段。
  • 你应该单独发布你的(格式很好的)sql,因为它是你问题的缩影,而不是.net代码
  • @T.S.谢谢,但我想通了。花了我一段时间,是的,这只是 sql 的问题。

标签: c# mysql .net inventory


【解决方案1】:

我能够用这个逻辑解决这个问题。

Statement 1: "SELECT initial_quantity from inventory_items Where item_name LIKE '" + selecteditem + "'; "

Statement 2: "SELECT COALESCE(SUM(Quantity), 0) from inventory_bookings Where item_name LIKE '" + selecteditem + "' AND '" + from_date + "'<= date_to AND date_from < '"+to_date+"'"

语句#1 选择项目的初始值数量。 (让我们说 3)。

语句 #2 选择在选定的日子里有多少物品(让我们说 2)

然后它检查(3-2)是否大于或等于所需数量,然后预订。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-04-07
    • 1970-01-01
    • 1970-01-01
    • 2015-02-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-11-01
    相关资源
    最近更新 更多