本文介绍了DataTable中的问题查找记录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有2张桌子:
Order(id,date,idWordker,idCustomer)Primakey键:id
OrderDetail(id,idProduct,quantity,price)Primakey键:id
您可以在OrderDetail中看到2个键.我需要更新数量
我使用:
I have 2 tables:
Order(id,date,idWordker,idCustomer) Primakey key:id
OrderDetail(id,idProduct,quantity,price) Primakey key:id
You can see 2 keys in OrderDetail.I need to update the quantity
I use:
public DataTable AddCart(string id, string idProduct,int quantity,int price)
{
DataTable tbCart = new DataTable();
tbCart = (DataTable)HttpContext.Current.Session["Cart"];
if (tbCart == null)
tbCart = MakeCart();
DataRow dr = tbCart.NewRow();
dv = tbCart.DefaultView;
dv.Sort = "idProduct";
rIndex = dv.Find(idProduct);
if (rIndex != -1)
{
//update quantity
}
else
{
dr[0] = id;
dr[1] = idProduct;
dr[2] = 1;//quantity
dr[3] = price;
}
}
如果OrderDetail存在idProduct,则产品数量将被更新.Session["Cart"]将被插入OrderDetail
如何用2个键找到?
If OrderDetail exist idProduct,the quantity of product will be updated.Session["Cart"] will be insert into OrderDetail
How to find with 2 keys?
推荐答案
DataRow[] rows = tbCart.Select(
string.Format("id='{0}' and idProduct='{1}'",id,idProduct),
idProduct, DataViewRowState.CurrentRows);
if (rows.Length > 0){
rows[0]["quantity"]=
rows[0]["price"]=
}
这篇关于DataTable中的问题查找记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!