C# SQL SUM value to a label

后端 未结 2 1361
半阙折子戏
半阙折子戏 2020-12-21 07:17

I currently have a DataGridView which displays all the item. I would like to sum all the prices in the price column and then reflect the total in a label, \'TotalValueLabel\

2条回答
  •  被撕碎了的回忆
    2020-12-21 07:28

    DataTable is overkill for single value retrieval, besides your not even accessing the value correctly, better way is to use execute scalar:

    var query = "SELECT SUM (Price) FROM Bill";
    using (var cmd = new OleDbCommand(query, DBcon))
    {
        TotalValueLabel.Text = cmd.ExecuteScalar().ToString();
    }
    

提交回复
热议问题