Этот вопрос аналогичен моему сообщению раньше: Previous post, но я задал этот вопрос, потому что я обнаружил новую проблему и не мог решить это сам.Отслеживание следа в базе данных
В данном случае: Я хочу проверить, есть ли Quantity
в базе данных меньше 5 и показать сообщение, но проблема в том, что в базе данных есть две базы данных, а Quantity
с первого - 3 и второй - 2, он отображает только сообщение, и в базе данных выбирается самое низкое значение Quantity
. Но когда Quantity
из DATAS в базе данных одинаковы, то появится сообщение, где Quantity
в базе данных меньше 5.
Вот образ базы данных:
Вот образ, когда есть два Datas в базе данных и 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. Огромное спасибо. –