【发布时间】:2014-12-30 02:00:54
【问题描述】:
这个问题与我之前的帖子类似:Previous post,但我提出这个问题是因为我发现了一个新问题并且我自己无法解决这个问题。
情况如下:我想检查数据库中的Quantity是否小于5并显示消息,但问题是当数据库中有两个数据和第一个数据的Quantity是 3,第二个是 2,它只显示消息,并且只选择数据库中最低的 Quantity 值。但是当数据库中Quantity的数据相同时,会显示数据库中Quantity小于5的消息。
这是数据库的图片:
这是当数据库中有两个数据并且两者的Quantity 和消息本身相同时的图像:
这是当数据库中有两个数据并且两者的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();
}
}
用户信息类:
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。非常感谢。