此問題與我之前的帖子類似:Previous post,但是我提出這個問題是因爲我發現了一個新問題,並且無法自己解決此問題。在數據庫中跟蹤數量
這裏是這樣的情況:我想檢查數據庫中的Quantity
是否小於5並顯示消息,但問題是當數據庫中有兩個數據並且第一個數字是Quantity
且第二個是2,它只顯示消息,並且它只是選取數據庫中最低的值Quantity
。但是,當數據庫中的數據更Quantity
是一樣的,它會顯示消息,其中Quantity
數據庫小於5
這裏是數據庫的圖像:
這裏是數據庫中有兩個數據時的圖像,它們的Quantity
都是相同的,並且消息本身是:
下面是當在所述數據庫中的兩個DATAS的圖像和Quantity
兩個它是不同的並且消息本身:
作爲您可以從上面的圖像中看到,該消息顯示兩個數據的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();
}
}
}
任何幫助,將不勝感激!
非常感謝!
抱歉,對方長時間發帖。
轉到調試。放置斷點,遍歷代碼。你會看到'ProductInfo.Quantity = 2',使您的查詢只返回1行。使用更少的靜態,更少的查詢並在一種方法中解決此問題。 – CodeCaster
我會的,先生@CodeCaster。非常感謝。 –