如何检查项目是否可用于特定日期范围

时间:2016-08-18 17:21:55

标签: c# mysql .net inventory

我正在使用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皮划艇(初始数量3)预订2016-08-19至2016-08-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();
        }

1 个答案:

答案 0 :(得分:0)

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

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)是否大于或等于所需数量,然后预订。