此问题与我之前的帖子类似: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。非常感谢。 –