• LINQ to SQL Update 方法


    1. 

     public void UpdateFoods()
    {
      using (FoodsDataContext context = new FoodsDataContext())
      {
        food firstFood = (from f in context.foods
          where f.id == 1
          select f).First<food>();
        firstFood.calories = 100;
        context.SubmitChanges();
      }
    }

    2.

    public bool UpdateCustomFood(food customFood)
    {
      bool result = false;
      using (DataLayerDataContext context = new DataLayerDataContect())
      {
        context.foods.Attach(customFood, true);
        try
        {
          context.SubmitChanges();
          result = true;
        }
        catch (ChangeConflictException e)
        {
          // Handle the change conflict here
        }
      return result;
      }
    }

    3.You can either pass in both a copy of the new object along with the original or you can choose to have LINQ to SQL ignore concurrency altogether. Here is the same update method that sends back a copy of the original object along with the updated version:

    public bool UpdateCustomFood(Food original, Food updated)
    {
      bool result = false;
      using (DataLayerDataContext context = new DataLayerDataContect())  
      {
        context.foods.Attach(original, false);
        original.name = updated.name;
        try
        {
          context.SubmitChanges();
          result = true;
        }
        catch (ChangeConflictException e)
        {
          // Handle the change conflict here
        }
        return result;
      }
    }

  • 相关阅读:
    Linux安装配置tomcat
    linux 安装jdk
    让/etc/profile文件修改后立即生效
    group by 和聚合函数
    mysql插入日期 vs oracle插入日期
    Thinkphp学习笔记-删除缓存
    Thinkphp错误-phpMyAdmin 尝试连接到 MySQL 服务器,但服务器拒绝连
    Thinkphp学习笔记-模板赋值
    Thinkphp学习笔记-模板主题
    Windows-设置系统服务不开机启动
  • 原文地址:https://www.cnblogs.com/YSO1983/p/1831482.html
Copyright © 2020-2023  润新知