每15秒检查一次数据库中的数量 [英] Check the quantity in the database every 15 seconds

查看:144
本文介绍了每15秒检查一次数据库中的数量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想问一个问题,我想在数据库中每15秒左右跟踪数量。它工作正常,但问题是它检查每个具有小于5的数量,而不是小于5的数量的单列。数据库如下图所示:

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:

从下面的图片,我减去数量从下面的图像到数据库(上面的图像),所以数据库2,每当第一行和第二行(下面的图像)中的数量小于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:

img src =https://i.stack.imgur.com/ZCs1W.pngalt =enter image description here>

问题是,数量在数据库的第一行或第二行中的数量仍然大于5或等于(例如:数据库第一行中的数量为2,但在第二行为50),该框在右上角如上图像不显示,只显示数据库中第一行和第二行中的数量小于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?

My question is: How do I show the box whenever either the quantity in the first or second row more than 5?

这里是我使用的代码:

系统管理员类:

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();
    }
}

用户信息类:

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:

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();
         }

     }
 }

非常感谢!

非常感谢!

推荐答案

循环:

while (reader.Read())
{
    int quantity = (int)reader["Quantity"];

    UserInformation.Quantity = Convert.ToDecimal(quantity);
}

您正在覆盖 UserInformation.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 类是定义的,所以很难显示如何修改它,但基本上,你应该只查询适用的行:

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:

string query = "SELECT [Product Code], [Quantity] FROM [Database] " +
               "WHERE [Quantity] < 5";

创建结果列表:

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;

然后您可以检查 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();
}

编辑:在评论中回答您的问题,一个简单的方法可以实现 ProductInfo 是:

to answer your question in the comments, one simple way you could implement ProductInfo is:

class ProductInfo
{
    public string Code { get; private set; }
    public int Quantity { get; private set; }

    public ProductInfo(string code, int quantity)
    {
        Code = code;
        Quantity = quantity;
    }
}

这篇关于每15秒检查一次数据库中的数量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆