如何向购物车表中插入多条数据,如果购物车中存在该数据,就只更新数量
1个回答
展开全部
/// <summary>
/// 批量添加产品属性库存
/// </summary>
public bool AddStock(string[] proidList, string[] proskuList, string[] countnumList, string[] priceList, string[] is_attribute,string[] riskofstocktips, string userid)
{
SqlConnection conn = new SqlConnection(Maticsoft.DBUtility.PubConstant.ConnectionString);
conn.Open();
SqlDataAdapter adapter = new SqlDataAdapter("select proid,prosku,countnum,allcountnum,price,userid,operatorid,riskofstocktips from Stock", conn);
SqlCommandBuilder builder = new SqlCommandBuilder(adapter);
DataSet ds = new DataSet();
adapter.Fill(ds);
ds.Tables[0].PrimaryKey = new DataColumn[] { ds.Tables[0].Columns["proid"] };//数据库里这个proid敢必然是主键才可
int allcountnum = 0;
//BLL.BLLStockAttribute bll = new BLL.BLLStockAttribute();
//string[] attrskulist,attrcountnumList,attrpriceList;
for (int i = 0; i < proidList.Length; i++)
{
if (is_attribute[i] == "0")//证明是没有属性列表的就直接加数量
{
if (countnumList[i] != "0" && Maticsoft.Common.PageValidate.IsNumber(countnumList[i]) && int.Parse(countnumList[i]) > 0)
{
//在ds.Tables[0]中查找数据库中是否已经存在该条数据
DataRow modyRow = ds.Tables[0].Rows.Find(int.Parse(proidList[i]));
//如果数据库已经存在该行,则修改记录,不进行插入
if (modyRow != null)
{
//设置该行记录为“已修改”状态,此操作必须。
//ds.Tables[0].Rows.Find(proidList[i]).SetModified();
DataRow dr = ds.Tables[0].Rows.Find(proidList[i]);
//重新给该记录赋值
dr.BeginEdit();
//dr[0] = proskuList[i];
dr[1] = proskuList[i];
if (dr[2].ToString() != "")
{
dr[2] = (int.Parse(countnumList[i].ToString()) + int.Parse(dr[2].ToString())).ToString();
}
else
{
dr[2] = countnumList[i];
}
if (dr[3].ToString() != "")
{
allcountnum = int.Parse(dr[3].ToString()) + int.Parse(countnumList[i].ToString());
}
dr[3] = allcountnum;
dr[4] = priceList[i];
dr[5] = dr[5];
dr[6] = userid;
dr[7] = riskofstocktips[i];
dr.EndEdit();
}
else
{
DataRow dr = ds.Tables[0].NewRow();
dr[0] = proidList[i];
dr[1] = proskuList[i];
dr[2] = countnumList[i];
dr[3] = countnumList[i];
dr[4] = priceList[i];
dr[5] = userid;
dr[6] = userid;
dr[7] = riskofstocktips[i];
ds.Tables[0].Rows.Add(dr);
}
}
//添加入库存操作明细
}
//else
//{
// //有属性列表的 对属性列表进行添加到库存的属性列表
// //attrskulist = Request.Form["proattrsku" + proidList[i]].ToString().Split(',');//属性sku
// //attrcountnumList = Request.Form["proattrnum" + proidList[i]].ToString().Split(',');//属性数量
// //attrpriceList = Request.Form["proattrprice" + proidList[i]].ToString().Split(',');//属性价钱
// //bll.AddStockAttribute(proskuList[i], attrskulist, attrcountnumList, attrpriceList, userid);
//}
}
adapter.Update(ds);
return true;
}
道理一样的,看不懂就算了
/// 批量添加产品属性库存
/// </summary>
public bool AddStock(string[] proidList, string[] proskuList, string[] countnumList, string[] priceList, string[] is_attribute,string[] riskofstocktips, string userid)
{
SqlConnection conn = new SqlConnection(Maticsoft.DBUtility.PubConstant.ConnectionString);
conn.Open();
SqlDataAdapter adapter = new SqlDataAdapter("select proid,prosku,countnum,allcountnum,price,userid,operatorid,riskofstocktips from Stock", conn);
SqlCommandBuilder builder = new SqlCommandBuilder(adapter);
DataSet ds = new DataSet();
adapter.Fill(ds);
ds.Tables[0].PrimaryKey = new DataColumn[] { ds.Tables[0].Columns["proid"] };//数据库里这个proid敢必然是主键才可
int allcountnum = 0;
//BLL.BLLStockAttribute bll = new BLL.BLLStockAttribute();
//string[] attrskulist,attrcountnumList,attrpriceList;
for (int i = 0; i < proidList.Length; i++)
{
if (is_attribute[i] == "0")//证明是没有属性列表的就直接加数量
{
if (countnumList[i] != "0" && Maticsoft.Common.PageValidate.IsNumber(countnumList[i]) && int.Parse(countnumList[i]) > 0)
{
//在ds.Tables[0]中查找数据库中是否已经存在该条数据
DataRow modyRow = ds.Tables[0].Rows.Find(int.Parse(proidList[i]));
//如果数据库已经存在该行,则修改记录,不进行插入
if (modyRow != null)
{
//设置该行记录为“已修改”状态,此操作必须。
//ds.Tables[0].Rows.Find(proidList[i]).SetModified();
DataRow dr = ds.Tables[0].Rows.Find(proidList[i]);
//重新给该记录赋值
dr.BeginEdit();
//dr[0] = proskuList[i];
dr[1] = proskuList[i];
if (dr[2].ToString() != "")
{
dr[2] = (int.Parse(countnumList[i].ToString()) + int.Parse(dr[2].ToString())).ToString();
}
else
{
dr[2] = countnumList[i];
}
if (dr[3].ToString() != "")
{
allcountnum = int.Parse(dr[3].ToString()) + int.Parse(countnumList[i].ToString());
}
dr[3] = allcountnum;
dr[4] = priceList[i];
dr[5] = dr[5];
dr[6] = userid;
dr[7] = riskofstocktips[i];
dr.EndEdit();
}
else
{
DataRow dr = ds.Tables[0].NewRow();
dr[0] = proidList[i];
dr[1] = proskuList[i];
dr[2] = countnumList[i];
dr[3] = countnumList[i];
dr[4] = priceList[i];
dr[5] = userid;
dr[6] = userid;
dr[7] = riskofstocktips[i];
ds.Tables[0].Rows.Add(dr);
}
}
//添加入库存操作明细
}
//else
//{
// //有属性列表的 对属性列表进行添加到库存的属性列表
// //attrskulist = Request.Form["proattrsku" + proidList[i]].ToString().Split(',');//属性sku
// //attrcountnumList = Request.Form["proattrnum" + proidList[i]].ToString().Split(',');//属性数量
// //attrpriceList = Request.Form["proattrprice" + proidList[i]].ToString().Split(',');//属性价钱
// //bll.AddStockAttribute(proskuList[i], attrskulist, attrcountnumList, attrpriceList, userid);
//}
}
adapter.Update(ds);
return true;
}
道理一样的,看不懂就算了
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询