2014-12-27 24 views
-1

我想問一個問題,我想每隔15秒左右在數據庫中跟蹤quantity ..它工作正常,但問題是它檢查每一列有quantity小於5中,具有小於5的quantity不單個列我有像下面的圖像的數據庫:查看數據庫中數據每15秒

enter image description here

而從下面的圖像,我減去從下面的圖像的數量數據庫(上圖),因此數據庫中的數量(上圖)現在爲2,只要第一行和第二行(圖像下方)的數量小於5,它就會在Bott OM右上角像下面圖像:

enter image description here

的問題是,無論是數據庫仍然超過5或等於(例如第一或第二行中的數量:的第一行中的數量數據庫是2,但在第二行是50),右上角的框如上圖所示,它只顯示數據庫中第一行和第二行的數量小於5.

我的問題是:無論第一行還是第二行的數量超過5個,我如何顯示該框?

這裏是我使用的代碼:

系統管理類:

public static void GetQuantity() 
{ 
    using (OleDbConnection connection = new OleDbConnection(connectionString)) 
    { 
     string query = "SELECT [Quantity] FROM [Database]"; 

     connection.Open(); 

     using (OleDbCommand command = new OleDbCommand(query, connection)) 
     { 
      using (OleDbDataReader reader = command.ExecuteReader()) 
      { 
       while (reader.Read()) 
       { 
        int quantity = (int)reader["Quantity"]; 

        UserInformation.Quantity = Convert.ToDecimal(quantity); 
       } 

       reader.Close(); 
      } 
     } 

     connection.Close(); 
    } 
} 

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

    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 = UserInformation.Quantity; 

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

        if (UserInformation.Quantity < 5) 
        { 
         message += "- Product Code: " + productCode + "\n- Quantity: " + UserInformation.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."; 

        if (UserInformation.Language == "Indonesian") 
        { 
         _message1 = "Program mendeteksi bahwa: \n\n"; 
         _message2 = "Memiliki kuantitas kurang dari 5.\nPerbarui segera."; 
        } 

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

       reader.Close(); 
      } 

     } 

     connection.Close(); 
    } 
} 

用戶信息類:

public static decimal Quantity 
     { 
      get; 
      set; 
     } 

主系統類:這裏我在哪裏打電話給方框並減去要求ntity從這個類到數據庫:

int timeLeft = 15; 

Timer _timer = new Timer(); 

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

    _timer.Tick += Timer_Tick; 

    _timer.Start(); 
} 

void Timer_Tick(object sender, EventArgs e) 
{ 
    timeLeft--; 

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

     MessageBox.Show("The timer has been stopped"); 

     SystemManager.GetQuantity(); 

     if (UserInformation.Quantity < 5) 
     { 
      MessageBox.Show("The quantity less than 5"); 

      SystemManager.CheckQuantity(customToolTip1, this, _screen.Right, _screen.Bottom, 5000); 

      timeLeft = 15; 

      _timer.Start(); 
     } 

     else if (UserInformation.Quantity >= 5) 
     { 
      MessageBox.Show("The quantity more than 5 or equal"); 

      timeLeft = 15; 

      _timer.Start(); 
     } 

     else 
     { 
      MessageBox.Show("Nothing"); 

      timeLeft = 15; 

      _timer.Start(); 
     } 

    } 
} 

您的回答非常感謝!

非常感謝!

+1

你能改寫這個問題?我不清楚你想達到什麼目的。 –

+0

這個主題根本沒有意義。你的頭銜說你有一個錯誤,你沒有在線程中顯示,我無法按照你想要做的事情的意圖。 – Samuel

+0

@LajosArpad:我編輯了這個問題先生,請讓我知道如果你仍然不清楚我正在努力達到什麼目的。謝謝 –

回答

3

在這個循環中:

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

    UserInformation.Quantity = Convert.ToDecimal(quantity); 
} 

你一遍又一遍覆蓋的UserInformation.Quantity的值,直到最後一排,和它保持任何值是最後一行。

你還沒有告訴我們您的UserInformation類是如何定義的,所以很難向你展示如何修改它,但基本上,你應該只查詢了適用行:

string query = "SELECT [Product Code], [Quantity] FROM [Database] " + 
       "WHERE [Quantity] < 5"; 

構建了結果的列表:

var lowProducts = new List<ProductInfo>(); 

while (reader.Read()) 
{ 
    int quantity = (int)reader["Quantity"]; 
    string code = (string)reader["Product Code"]; 

    lowProducts.Add(new ProductInfo(code, quantity));  
} 

UserInformation.LowProducts = lowProducts; 

然後你就可以檢查是否LowProducts有任何項目:

if (UserInformation.LowProducts.Any()) 
{ 
    MessageBox.Show("Some products are running low."); 

    SystemManager.CheckQuantity(customToolTip1, this, _screen.Right, 
           _screen.Bottom, 5000); 
    timeLeft = 15; 
    _timer.Start(); 
} 

編輯:回答你的問題的評論,你可以實現一個ProductInfo簡單的方法是:

class ProductInfo 
{ 
    public string Code { get; private set; } 
    public int Quantity { get; private set; } 

    public ProductInfo(string code, int quantity) 
    { 
     Code = code; 
     Quantity = quantity; 
    } 
} 
+0

謝謝您的回答sir @JLRishe,但UserInformation.LowProducts是什麼?我似乎沒有找到'.Any()',而我把'UserInformation.LowProducts'的getter和setter設置爲'decimal'。謝謝。什麼是'ProductInfo先生?它包含哪些數據?非常感謝 –

+0

@UnknownUser LowProducts將是一個'IEnumerable '('ProductInfo'是一個用於存放產品信息的類)。由於可以有多個產品的數量較少,因此您需要獲取其中的一個,而不僅僅是一個。只需在UserInformation.Quantity中存儲一個產品就是問題的全部來源。 – JLRishe

+0

非常感謝您的先生,我會馬上嘗試 –