2014-12-29 139 views
-2

此問題與我之前的帖子類似:Previous post,但是我提出這個問題是因爲我發現了一個新問題,並且無法自己解決此問題。在數據庫中跟蹤數量

這裏是這樣的情況:我想檢查數據庫中的Quantity是否小於5並顯示消息,但問題是當數據庫中有兩個數據並且第一個數字是Quantity且第二個是2,它只顯示消息,並且它只是選取數據庫中最低的值Quantity。但是,當數據庫中的數據更Quantity是一樣的,它會顯示消息,其中Quantity數據庫小於5

這裏是數據庫的圖像:

Database

這裏是數據庫中有兩個數據時的圖像,它們的Quantity都是相同的,並且消息本身是:

Same Quantity for datas in the database

下面是當在所述數據庫中的兩個DATAS的圖像和Quantity兩個它是不同的並且消息本身:

enter image description here

作爲您可以從上面的圖像中看到,該消息顯示兩個數據的Quantity都是相同的數據。

我該如何解決這個問題?

這裏是我使用(的幫助從@JLRishe從以前的帖子)的代碼:

SystemManager類:

public static void GetProductInfo() 
     { 
      using (OleDbConnection conn = new OleDbConnection(connectionString)) 
      { 
       string query = "SELECT [ProductCode], [Quantity] FROM [Database] WHERE [Quantity] < 5"; 

       conn.Open(); 

       using (OleDbCommand command = new OleDbCommand(query, conn)) 
       { 
        using (OleDbDataReader reader = command.ExecuteReader()) 
        { 
         var lowQuantity = new List<ProductInfo>(); 

         while (reader.Read()) 
         { 
          string productCode = (string)reader["ProductCode"]; 
          int quantity = (int)reader["Quantity"]; 

          lowQuantity.Add(new ProductInfo(productCode, quantity)); 
         } 

         UserInformation.LowQuantity = lowQuantity; 
        } 
       } 

      } 
     } 

     public static void CheckQuantity(CustomToolTip _customToolTip, IWin32Window _window, int _x, int _y, int _duration) 
     { 
      GetProductInfo(); 

      string message = string.Empty; 

      string productCode = string.Empty; 

      using (OleDbConnection connection = new OleDbConnection(connectionString)) 
      { 
       string query = "SELECT [ProductCode] FROM [Database] WHERE [Quantity] = @Quantity ORDER BY [ProductCode] ASC"; 

       connection.Open(); 

       using (OleDbCommand command = new OleDbCommand(query, connection)) 
       { 
        command.Parameters.Add("@Quantity", OleDbType.Decimal); 
        command.Parameters["@Quantity"].Value = ProductInfo.Quantity; 

        using (OleDbDataReader reader = command.ExecuteReader()) 
        { 
         while (reader.Read()) 
         { 
          productCode = (string)reader["ProductCode"]; 

          /*if (ProductInfo.Quantity < 5) 
          { 
           message += "- Product Code: " + productCode + "\n- Quantity: " + ProductInfo.Quantity + "\n\n"; 
          }*/ 

          if (UserInformation.LowQuantity.Any()) 
          { 
           message += "- Product Code: " + productCode + "\n- Quantity: " + ProductInfo.Quantity + "\n\n"; 
          } 
         } 

         if (message != string.Empty) 
         { 
          SystemManager.SoundEffect(@"\Media\Speech Off.wav"); 

          string _message1 = "The system has detected the following: \n\n"; 
          string _message2 = "Have quantity less than 5.\nPlease update them immediately."; 

          _customToolTip.Show(_message1 + message + _message2, _window, _x, _y, _duration); 
         } 

         reader.Close(); 
        } 

       } 

       connection.Close(); 
      } 
     } 

UserInformation類:

public static IEnumerable<ProductInfo> LowQuantity 
     { 
      get; 
      set; 
     } 

ProductInfo類:

public static string Code 
     { 
      get; 
      set; 
     } 

     public static int Quantity 
     { 
      get; 
      set; 
     } 

     public ProductInfo(string _code, int _quantity) 
     { 
      Code = _code; 
      Quantity = _quantity; 
     } 

MainSystem形式:(這裏是我執行的代碼)

Timer _timer = new Timer(); 

int timeLeft = 15; 

void MainSystem_Load(object sender, EventArgs e) 
     { 
      _timer.Interval = 1000; 

      _timer.Tick += Timer_Tick; 

      _timer.Start(); 
     } 

void Timer_Tick(object sender, EventArgs e) 
     { 
      this.textBox4.Text = DateTime.Now.ToString("dd - MMM - yyyy hh:mm:ss tt"); 

      timeLeft--; 

      if (timeLeft == 0) 
      { 
       _timer.Stop(); 

       SystemManager.GetProductInfo(); 

       if (UserInformation.LowQuantity.Any()) 
       { 
        SystemManager.CheckQuantity(customToolTip1, this, _screen.Right, _screen.Bottom, 5000); 

        timeLeft = 15; 

        _timer.Start(); 
       } 

       else 
       { 
        timeLeft = 15; 

        _timer.Start(); 
       } 

      } 
     } 

任何幫助,將不勝感激!

非常感謝!

抱歉,對方長時間發帖。

+0

轉到調試。放置斷點,遍歷代碼。你會看到'ProductInfo.Quantity = 2',使您的查詢只返回1行。使用更少的靜態,更少的查詢並在一種方法中解決此問題。 – CodeCaster

+0

我會的,先生@CodeCaster。非常感謝。 –

回答

2

在你CheckQuantity()方法,你是:

  1. 有,你就構造了第一ProductInfo相同數量只有查詢項目。
  2. 顯示從單一ProductInfo,而不是你從數據庫中查詢值的數量。

相反,這應該更好的工作:

public static void CheckQuantity(CustomToolTip _customToolTip, IWin32Window _window, 
           int _x, int _y, int _duration) 
{ 
    string message = string.Empty; 

    string productCode = string.Empty; 
    int quantity; 

    string query = 
     "SELECT [ProductCode], [Quantity] FROM [Database] " + 
     "WHERE [Quantity] = < 5 ORDER BY [ProductCode] ASC"; 
    using (OleDbConnection connection = new OleDbConnection(connectionString)) 
    using (OleDbCommand command = new OleDbCommand(query, connection)) 
    using (OleDbDataReader reader = command.ExecuteReader()) 
    { 
     while (reader.Read()) 
     { 
      productCode = (string)reader["ProductCode"]; 
      quantity = (int)reader["Quantity"]; 

      message += "- Product Code: " + productCode + 
         "\n- Quantity: " + quantity + "\n\n"; 
     } 
     reader.Close(); 
     connection.Close(); 
    } 

    if (message != string.Empty) 
    { 
     SystemManager.SoundEffect(@"\Media\Speech Off.wav"); 

     string _message1 = "The system has detected the following: \n\n"; 
     string _message2 = "Have quantity less than 5.\nPlease update them immediately."; 

     _customToolTip.Show(_message1 + message + _message2, _window, 
          _x, _y, _duration); 
    } 
} 

我覺得你的代碼可能需要一些重組。你有一個查詢,檢查產品是否有一定量的下面,然後你一遍詢問他們重新獲得您已經檢索到的信息。我建議對此事進行一些思考,並找到一種方法,只用一個查詢就可以做到這一點。

+0

謝謝你這麼多先生@JLRishe,我會找到一個方法來簡化這些代碼,只有一個方法,只有一個查詢。我沒有先在論文中做過計劃(我只能從我的大腦和想法中想出),這可能是我最大的錯。 –