开发者

Calculating using SQL or C# - Multiple Columns & Rows

I have a table, and it has the Columns Quantity, Price and LineTotal. When I add something to the Cart, I add the Quantity and the Price of the product, and when the user visits the Cart page to view their cart, I want to re-calculate the LineTotal when they update the quantities for the items t开发者_JS百科hey've selected.

Now, my question is, should I re-calculate the LineTotal of each item in the cart using SQL (and if so, how?) or, should I do it in C# (And if so, what would be the best way to go about this?) To my surprise, I can't really seem to find anything on calculations in SQL - other than forums where people talk about it, but I have yet to see any code or documentation.


You should be able to use the arithmetic operators within SQL to generate your line totals. Reference here.


Well if you wanted to you could change the LineTotal column to a computed column but there's nothing stopping you from doing it in C# before the Insert/update

Since it seems to be a fairly simple calculation its tough to really strongly go with one or the other, and its mostly up to preference

Computed Column sample

ALTER TABLE dbo.YourTable
   ADD LineTotalComputed AS (Quantity * Price) PERSISTED
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜