DataTable中的问题查找记录 [英] Problem find record in DataTable

查看:78
本文介绍了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?

推荐答案

2个键?

其中哪一个是产品的唯一参考点?根据您的数据库设计和使用情况选择并使用唯一的一种.如果两者都是唯一的,请删除一个不需要的键.
2 keys?

Which one of them is a unique reference point for a product? Pick and use the one that is unique based on your DB design and use. If both are unique, remove one key as it''s not needed.


如问题中所述,只有Id OrderDetail 中的主键,而idProduct 是另一个id.
因此,我认为DataTable Select 方法可用于查找所需的行,如下所示:
As stated in the question only Id is the primary key in OrderDetail and idProduct is another id.
Hence, I think the Select method of DataTable can be used to find the required row as follows:
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中的问题查找记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆