日期:2014-05-20  浏览次数:20877 次

linq中的插入数据,应该调用什么方法?
/// <summary>
  /// 
  /// 更新商品
  /// </summary>
  /// <param name="good"></param>
  public static void UpdateGood(Good good)
  {
  using (GoosDBDataContext dataContext = new GoosDBDataContext()) 
  { 
  //查出要更新的商品
  Good data = dataContext.Good.Where(item => item.GoodsId == good.GoodsId).SingleOrDefault();
  //商品名字处理
  data.GoodsName = good.GoodsName;
  //商品类型外键处理
  data.GoodsType = dataContext.GoodsType.Where(item => item.TypeId == good.TypeId).SingleOrDefault();
  data.Price = good.Price;
  //商品单位外键处理
  data.GoodsUnit = dataContext.GoodsUnit.Where(item => item.UnitId == good.UnitId).SingleOrDefault();
  data.ProduceDate = good.ProduceDate;

  //提交更新
  dataContext.SubmitChanges();
  }
  }
  /// <summary>
/// 添加商品方法
/// </summary>
/// <param name="good"></param>
public static void AddGood(Good good)
{
using (GoodSys.Models.GoosDBDataContext dataContext = new GoosDBDataContext()) {

//到这里写不下去了!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!1
Good data = good;



}
}

------解决方案--------------------
C# code
public static void AddGood(Good good)
{
if(good== null)return;
//检查字段合法性。。。
using (GoodSys.Models.GoosDBDataContext dataContext = new GoosDBDataContext()) {
   dataContext.InsertOnSubmit(good);
   dataContext.SubmitChanges();
}
}