每15秒检查一次数据库中的数量

时间:2014-12-27 10:24:42

标签: c# sql .net database winforms

我想问一个问题,我想每15秒左右继续跟踪数据库中的quantity ..它工作正常,但问题是检查每个列{{1}小于5,而不是quantity的单列少于5.我的数据库如下图所示:

enter image description here

从下面的图片中,我将数量从下面的图像减去数据库(上图),因此,只要第一行和第二行中的数量(图像下方),数据库中的数量(上图)现在为2 )小于5,它将显示右下角的框,如下图所示:

enter image description here

问题是,数据库的第一行或第二行中的数量是否仍然大于5或相等(例如:数据库第一行中的数量为2,但第二行中的数量为50),如上图所示,右下角的框不显示,只显示数据库中第一行和第二行的数量小于5时。

我的问题是:当第一行或第二行的数量超过5时,如何显示该框?

以下是我正在使用的代码:

System Manager类:

quantity

用户信息类:

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

你的回答非常感谢!

非常感谢你!

1 个答案:

答案 0 :(得分:3)

在这个循环中:

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

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

你一遍又一遍地覆盖UserInformation.Quantity的值,直到最后一行,它保留最后一行中的任何值。

您尚未向我们展示如何定义UserInformation类,因此很难向您展示如何修改它,但基本上,您应该只查询适用的行:

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

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

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