[英]Check the quantity in the database every 15 seconds
I want to ask a question, I want to keep tracking the quantity
in the database every 15 seconds or so.. It works fine, but the problem is it check the every column that have quantity
less than 5, not single column of quantity
that have less than 5. I have the database like the image below:我想问一个问题,我想每隔 15 秒左右跟踪数据库中的quantity
.. 工作正常,但问题是它检查quantity
小于 5 的每一列,而不是单列quantity
少于 5 个。我有如下图所示的数据库:
And from the image below, I minus the Quantity from the below image to the database (above image), so the quantity in the database (above image) is now 2, whenever the Quantity in first and second row (below image) are less than 5, it will show the box in the bottom right corner like the below image:从下图中,我将下图的数量减去到数据库(上图),因此数据库中的数量(上图)现在为 2,只要第一行和第二行中的数量(下图)较少超过 5,它将在右下角显示框,如下图所示:
The problem is, either the quantity in the first or second row of the database still more than 5 or equal (for example: the quantity in the first row of the database is 2, but in the second row is 50), the box in the bottom right corner like above image does not show, it only shows when the quantity in first and second row in the database less than 5.问题是,要么数据库第一行或第二行的数量仍然大于或等于5(例如:数据库第一行的数量是2,而第二行的数量是50),则在右下角如上图不显示,只在数据库第一行和第二行的数量小于5时显示。
My question is: How do I show the box whenever either the quantity in the first or second row more than 5?我的问题是:当第一行或第二行中的数量超过 5 时,如何显示该框?
Here is the code that I am using:这是我正在使用的代码:
System Manager class:系统管理员类:
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();
}
}
User Information class:用户信息类:
public static decimal Quantity
{
get;
set;
}
Main System class: Here is where I called the box and minus the quantity from this class to the database: Main System 类:这里是我调用 box 并减去从此类到数据库的数量的地方:
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();
}
}
}
Your answer much appreciated!非常感谢您的回答!
Thank you so much!非常感谢!
In this loop:在这个循环中:
while (reader.Read())
{
int quantity = (int)reader["Quantity"];
UserInformation.Quantity = Convert.ToDecimal(quantity);
}
you are overwriting the value of UserInformation.Quantity
over and over until the last row, and it keeps whatever value is in the last row.您一遍又一遍地覆盖UserInformation.Quantity
的值,直到最后一行,它会保留最后一行中的任何值。
You haven't shown us how your UserInformation
class is defined, so it's hard to show you how to modify it, but basically, you should only be querying for rows that are applicable:您还没有向我们展示您的UserInformation
类是如何定义的,因此很难向您展示如何修改它,但基本上,您应该只查询适用的行:
string query = "SELECT [Product Code], [Quantity] FROM [Database] " +
"WHERE [Quantity] < 5";
Build up a list of the results:建立一个结果列表:
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;
And then you can check whether LowProducts
has any items in it:然后你可以检查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();
}
Edit: to answer your question in the comments, one simple way you could implement ProductInfo
is:编辑:要在评论中回答您的问题,您可以实现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;
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.