2014-12-29 95 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,我会找到一个方法来简化这些代码,只有一个方法,只有一个查询。我没有先在论文中做过计划(我只能从我的大脑和想法中想出),这可能是我最大的错。 –