日期:2014-05-18  浏览次数:20432 次

asp.net 程序中,为什么把一条数据插入sql server数据库中,会连续两次把相同一条数据插入到表中
public ActionResult SelectById(ModelBuyIndent MB, FormCollection f)
  {
  btn.Add(new ModelButton { btnPower = "W", btnText = "入库", btnClass = "", btnFunction = "toSubmit(this)", btnName = "Sav", btnId = "Sav" });
  ViewData["ButList"] = new SDSButton().GetButton("DetStorage", "Stock", "", btn);
  //根据采购订单ID获取数据
  ModelBuyIndent Lk = BS.GetAllStock(MB.Id);
  foreach (var item1 in Lk.BuyIndentList)
  {
  if (item1.Num > item1.StockPNum)
  {
  //根据ID获取仓库名
  Storage S = BS.SelectByIdStorage(MB.Storage.StorageId);
  #region 添加库存单据表
  StockBill sb = new StockBill();
  sb.SBillType = 1;
  sb.SupplierId = Lk.SupplierId;
  sb.SupplierName = Lk.SupplierName;
  sb.SBillDate = MB.Time;
  sb.SBillTotal = decimal.Parse(f["zh"].ToString());
  sb.RecordId = (User.Identity as SDSIdentity).ID;
  sb.RecordName = (User.Identity as SDSIdentity).UName;
  sb.RecordTime = DateTime.Now.ToLocalTime();
  sb.UpdateId = (User.Identity as SDSIdentity).ID;
  sb.UpdateName = (User.Identity as SDSIdentity).UName;
  sb.UpdateTime = DateTime.Now.ToLocalTime();
  StockBill TF = BS.AddStockBill(sb);
  new BllLog().Save("DetStorage", "Stock", "", 3, "DET_StockBill", "添加库存单据(单据编号:" + sb.SBillNo + ")");
  #endregion

  #region 添加库存
  if (TF.SBillId > 0)
  {
  //添加库存单的每条商品
  foreach (ModelBuyIndentList item in MB.BuyIndentList)
  {
  ModelBuyIndentList ll = Lk.BuyIndentList.First(m => m.Id == item.Id);
  if (ll.Num > ll.StockPNum)
  {
  List<Stock> ListS = BS.SelectAllStock(null, item.Id, null, null, null, null, null, null, 1);
  Stock s = new Stock();
  s.SBillId = TF.SBillId;
  s.StorageId = MB.Storage.StorageId;
  if (ListS.Count != 0)
  {
  s.StockBatch = (ListS.Count + 1).ToString();
  }
  else
  {
  s.StockBatch = "1";
  }
  s.StockNum += item.LNum;
  s.StockType = 1;
  s.OrderId = ll.BIndentId;
  s.ProductId = ll.ProductId;
  s.ProductNo = ll.ProductNo;
  s.ProductName = ll.ProductName;
  s.ProductUnit = ll.Product.ProductUnit;
&nbs