【问题标题】:Check the quantity in the database every 15 seconds每15秒检查一次数据库中的数量
【发布时间】:2014-12-27 10:24:42
【问题描述】:

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

从下图中,我减去从下图到数据库(上图)的数量,所以数据库中的数量(上图)现在是 2,只要第一行和第二行中的数量(下图) ) 小于 5,它将在右下角显示如下图所示的框:

问题是,要么数据库第一行或者第二行的数量还是大于5或者相等(比如:数据库第一行的数量是2,但是第二行的数量是50),如上图右下角的框不显示,仅在数据库中第一行和第二行的数量小于5时显示。

我的问题是:当第一行或第二行的数量超过 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;
        }

主系统类:这里是我调用盒子的地方,减去从这个类到数据库的数量:

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

     }
 }

非常感谢您的回答!

非常感谢!

【问题讨论】:

  • 你能重新表述一下这个问题吗?我不清楚你想达到什么目标。
  • 这个话题完全没有意义。你的标题说你有一个错误,你没有在线程本身中显示,我无法理解你的意图。
  • @LajosArpad:我已经编辑了问题,先生,如果您仍然不清楚我想要达到的目标,请告诉我。谢谢
  • @Samuel:我已经提到了问题中的错误,并且我已经更新了问题先生,如果您仍然不清楚我想要达到的目标,请告诉我。谢谢

标签: c# sql .net database winforms


【解决方案1】:

在这个循环中:

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

编辑:要在 cmets 中回答您的问题,您可以实现 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;
    }
}

【讨论】:

  • 感谢@JLRishe 先生的回答,但UserInformation.LowProducts? 是什么?当我将UserInformation.LowProducts 的getter 和setter 设置为decimal 时,我似乎没有找到.Any()。谢谢你。什么是`ProductInfo先生?其中包含哪些数据?非常感谢
  • @UnknownUser LowProducts 将是一个IEnumerable&lt;ProductInfo&gt;ProductInfo 是一个保存产品信息的类)。由于可能存在不止一种数量较少的产品,因此您需要获得全部,而不仅仅是一种。仅将一种产品存储在 UserInformation.Quantity 中就是问题的全部根源。
  • 您好,先生,我仍然对在ProductInfo 课堂上做什么感到困惑,所以我做到了,您能再帮我一次吗?我已将此问题的代码上传到 Dropbox,我会将链接分享给您。先生,请看一下并再次帮助我。非常感谢。这是链接:Link
  • @UnknownUser 请看我上面的修改。
  • 您好,先生,我发现了一个新问题。假设数据库中有两个数据具有不同的Quantity,第一个是 2,第二个是 3.. 消息将显示数据库中数据中最低的 Quantity,而不是两个数据(因为Quantity 两者都小于 5)。我已经发布了一个新问题,而不是先生。如果我没有打扰您,请看一下先生:Post
猜你喜欢
  • 2022-01-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-07-27
  • 2014-06-05
  • 2012-04-19
相关资源
最近更新 更多