Can SqlParameter
only be used with SqlCommand
? I would like to use parameters within the DataTable.Compute()
function, but I'm not sure exactly how to do that correctly.
Here is my partial function:
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);
// .
// .
// .
}
}
This is simple as you proceed
There are several ways to handle this issue. As you proceeded with something I just carry it to end.
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;
}
Implemented the above code on .Net Framework 4.7.2