2016-08-18 6 views
0

SQL DB를 사용하여 C# 인벤토리 응용 프로그램을 작성합니다. 이 앱을 사용하면 예약 할 항목과 예약 가능한 항목을 선택할 수 있습니다. 꽤 쉬운 소리!, 내가 생각했던 것도 그거야. 여기에 문제가있다. 이 응용 프로그램은 두 가지 조건을 확인해야합니다. (필요한 수량을 사용할 수있는 경우 8 월 18 일)특정 날짜 범위에서 항목을 사용할 수 있는지 확인하는 방법

2) 당당한 23 일에 예 : 항목 (예 : 특정 날짜 범위 사이에서 사용할 수있는 경우

1) 2 또는 3).

하나의 데이터베이스에 항목 이름과 초기 수량이 있습니다. 현재 예약을 유지하는 또 다른 사람. 다음 데이터가 있습니다.

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

이제 2016-08-19부터 2016-08-23까지 1 카약 (초기 수량 3)을 예약하면됩니다. SQL로 어떻게 할 수 있습니까? 내가 지금까지하고있는 중이 Heres는 무엇

하지만이 논리에 이것을 알아낼 수 있었다 행운

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(); 
     } 
+0

사이드 노트 - 매개 변수가있는 쿼리를 사용하십시오! 선택한 목록 상자 항목의 값을 쿼리에 직접 붙여 넣어 취약점을 도입 할 수 있습니다. SQL 인젝션에 대해 읽어보십시오. – Raxr

+0

나는 항상 매개 변수화 된 쿼리를 고수한다. 이는 개발 단계에만 해당됩니다. –

+0

SQL 코드 –

답변

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) 포함하는지 이상 또는 필요한 수량과 동일하고이를 책 밖으로 선택한다.

관련 문제