【问题标题】:Quantity tracking in the database数据库中的数量跟踪
【发布时间】: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。非常感谢。

标签: c# database winforms


【解决方案1】:

在您的 CheckQuantity() 方法中,您是:

  1. 仅查询与您正在构建的第一个 ProductInfo 数量相同的项目。
  2. 显示来自该单个 ProductInfo 的数量,而不是您从数据库中查询的值。

相反,这应该会更好:

public static void CheckQuantity(CustomToolTip _customToolTip, IWin32Window _window, 
                                 int _x, int _y, int _duration)
{
    string message = string.Empty;

    string productCode = string.Empty;
    int quantity;

    string query =
        "SELECT [ProductCode], [Quantity] FROM [Database] " +
        "WHERE [Quantity] = < 5 ORDER BY [ProductCode] ASC";
    using (OleDbConnection connection = new OleDbConnection(connectionString))
    using (OleDbCommand command = new OleDbCommand(query, connection))
    using (OleDbDataReader reader = command.ExecuteReader())
    {
        while (reader.Read())
        {
            productCode = (string)reader["ProductCode"];
            quantity = (int)reader["Quantity"];

            message += "- Product Code: " + productCode + 
                       "\n- Quantity: " + quantity + "\n\n";
        }
        reader.Close();
        connection.Close();
    }

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

我认为您的代码可能需要重新组织。您有一个查询来检查产品是否低于某个数量,然后您再次查询它们以重新获取您已经检索到的信息。我建议对这个问题进行一些思考,并找到一种只需一个查询即可完成此任务的方法。

【讨论】:

  • 非常感谢@JLRishe 先生,我会找到一种方法来简化这些代码,只需一个方法和一个查询。我没有先在纸上计划就做了这个(我只是从我的大脑和想法中想象),这可能是我最大的错。
猜你喜欢
  • 2018-07-07
  • 2016-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-09-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-12-30
相关资源
最近更新 更多