私はSQL DBを使用してC#Inventoryアプリケーションをビルドしています。このアプリを使用すると、予約されるアイテムと、そのアイテムが予約される場合、アイテムを選択することができます。かなりイージー!、それは私が同様に考えたものです。ここに問題があります。 このアプリケーションは2つの条件を確認する必要があります。特定の日付範囲にアイテムがあるかどうかを確認する方法
1)アイテムが特定の日付範囲(例:間で利用可能である場合:)2、または3:8月23日8月18日)
2)必要な量は、例えば、(利用可能な場合。
私は1つのデータベースにアイテム名と初期数量を保持しています。現在の予約を保持するもう1つ。データには以下のものがあります。
ID Cottage quantity From Date To Date Item name
2 Woodcastle 2 2016-08-18 2016-08-24 Kayaks
は今、私は2016年8月19日から2016年8月23日に予約する1 Kayakの(初期量3)を選択した場合。どのように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インジェクションについて少し読んでください。 – Raxr
私は常にパラメータ化されたクエリに固執します。これは開発段階のみです。 –
SQLコード –