SqlParameter
只能与SqlCommand
一起使用吗?我想在 DataTable.Compute()
函数中使用参数,但我不确定如何正确执行此操作。
这是我的部分功能:
private void UpdateDeptBudget(int departmentId, int yearCode, int orderNum)
{
// get the order total
using (conn)
{
DataSet ds = new DataSet();
DataTable dt = ds.Tables["OrderItems"];
object orderTotal = dt.Compute("SUM(itemTotal)", "orderId = " + orderNum);
// .
// .
// .
}
}
这很简单,您继续进行即可
private void btnDataTableCompute_Click(object sender, EventArgs e)
{
DataTable dt = new DataTable();
dt.Columns.AddRange(new DataColumn[5]
{
new DataColumn("OrderID", typeof(int)),
new DataColumn("ProductName", typeof(string)),
new DataColumn("Qty", typeof(int)),
new DataColumn("Price", typeof(int)),
new DataColumn("Amount", typeof(int))
});
dt.Rows.Add(1, "TV", 1, 45000, 4500);
dt.Rows.Add(1, "Mobile", 1, 32000, 32000);
dt.Rows.Add(1, "Laptop", 2, 90000, 180000);
dt.Rows.Add(2, "Keyboard", 2, 3300, 6600);
dt.Rows.Add(2, "WashingMachine", 3, 1800, 5400);
decimal TotVal = OrderBalance(2, dt);
}
private static decimal OrderBalance(int mOrderID, DataTable dtOrder)
{
Decimal TotalValue = dtOrder.AsEnumerable().Where(row => row.Field<int>("OrderID") == mOrderID).Sum(row => row.Field<int>("Amount"));
return TotalValue;
}
在.Net Framework 4.7.2上实现上述代码