简体   繁体   English

我如何在 C# 中创建一种方法或 function 可以减少数据库中的库存,因为它正在超市出售

[英]How do i create a method or function in C# that can reduce the stock in the database as it is being sold in the supermarket

Am developing a C# windows desktop application in Visual Studio and am literally stack with regards to how i should phrase my code when it has been sold in my supermarket system so that it reduces the available stock in the database any assistance rendered will be much appreciated I tried with this Sql but it failed miserably cause i didnt even have the know the C# to add to it我正在 Visual Studio 中开发 C# windows 桌面应用程序,并且在我的超市系统中出售我的代码时,我应该如何措辞我的代码,以便它减少数据库中的可用库存,任何提供的帮助将不胜感激我尝试使用这个 Sql 但它失败了,因为我什至不知道要添加到它的 C#

    string query = "update ProductTable set Product_Quantity = (Product_Quantity-'{OrderProductQuantity}') where Product_Name ='{productName}'";

The answer?答案? Is you don't write any code at all to reduce the stock.您是否根本不编写任何代码来减少库存。

A simple query will get you the current stock一个简单的查询将为您提供当前库存

WITH MYstock as
(SELECT StockID, 
  (SELECT SUM(StockSold) from Invoices WHERE Invoices.StockID = Stock.StockDI) as ItemsSold, 
 (SELECT SUM(StockAdded) from tblStock WHERE TblStock.StockID) AS ItemsAdded)
 FROM tblSTock
)
SELECT * from MySTock, (ItemsAdded - ItemsSold) as InventoryLevel

So, as you can see, you don't have to write any code at all.因此,如您所见,您根本不需要编写任何代码。 You simple sum the items added, less the items sold, and you are done.您只需将添加的物品相加,减去已售出的物品,您就完成了。

Now, above is SQL server syntax, and you could say use a MySQL view - I don't know how (or if) aliased columns can be re-used, but above gives you the basic idea here.现在,上面是 SQL 服务器语法,您可以说使用 MySQL 视图 - 我不知道如何(或是否)可以重新使用别名列,但上面为您提供了基本的想法。

Really nice?非常好? Maybe you have to edit a sales transaction, or even delete a row - it will automatic update the inventory value - since you compute it on the fly as required.也许您必须编辑销售交易,甚至删除一行 - 它会自动更新库存值 - 因为您可以根据需要动态计算它。

As a result, your UI code becomes VERY easy to write.结果,您的 UI 代码变得非常容易编写。 You never have to write ANY code to update the inventory amounts, since you are free to calculate the in stock value anytime you want based on above.您无需编写任何代码来更新库存数量,因为您可以根据上述内容随时计算库存价值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM