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

LINQ SubmitChanges 方法不更新数据库
源码如下
 public static string Select(int hotelid)
  {
  using (HotelDataContext hcon = new HotelDataContext())
  {
  var hotelinfo = hcon.hotel.SingleOrDefault<hotel>(s=>s.hotelid==hotelid);
  return hotelinfo.hotelname;
  }
  }
  public static void Edit(int hotelid)
  {
  using (HotelDataContext hcon = new HotelDataContext())
  {
  var hotelinfo = hcon.hotel.SingleOrDefault<hotel>(s=>s.hotelid==hotelid);
  if (hotelinfo == null)
  {
  //===============
  }
  hotelinfo.hotelname = "修改后的酒店名";
  hcon.SubmitChanges();
  }  
  }

 Edit(81408);
  div1.InnerHtml = Select(81408);

更新不了 小弟 菜鸟 望高人不吝赐教!!不胜感激。

------解决方案--------------------
C# code

public static string Select(int hotelid)
  {
  HotelDataContext hcon = new HotelDataContext();
  
  var hotelinfo = hcon.hotel.SingleOrDefault<hotel>(s=>s.hotelid==hotelid);
  return hotelinfo==null?"":hotelinfo.hotelname;
  
  }
  public static void Edit(int hotelid)
  {
  HotelDataContext hcon = new HotelDataContext();
  
  var hotelinfo = hcon.hotel.SingleOrDefault<hotel>(s=>s.hotelid==hotelid);
  if (hotelinfo != null)
  {
     hotelinfo.hotelname = "修改后的酒店名";
      hcon.SubmitChanges();
  }
  
  }   

 Edit(81408);
  div1.InnerHtml = Select(81408);

------解决方案--------------------
单步调试
------解决方案--------------------
又来了哦。。。还是老问题,检查mdf文件是不是在工程里,然后每次编译都覆盖掉上次插入的数据了。
------解决方案--------------------
探讨

又来了哦。。。还是老问题,检查mdf文件是不是在工程里,然后每次编译都覆盖掉上次插入的数据了。

------解决方案--------------------
你的数据库ID是不是有主健?没主健是不能UPate的
------解决方案--------------------
可以这样
C# code

 var hotelinfo=(from h in hcon.hotel
               select h).FirstOrDefault();  
     
        hotelinfo.hotelname = "修改后的酒店名";
  hcon.SubmitChanges();

------解决方案--------------------
代码上看是没有问题。。。

你确认debug时两次hotelname结果不一样吗?


------解决方案--------------------
1.确认是否你的model和数据库的表不一样是否有过变动
2.确认你的数据库文件是不是在项目中被复制覆盖了
3.确认你的连接字符串,Try一下 submitchanges() 试试看
------解决方案--------------------
你好 你的问题解决了吗 可能是你把Select函数定义成了 静态的函数 把static 去掉看下
我也不是很清楚 可能是static 那个HotelDataContext只会加载以前的 只有重新编译才能得到更新的内容。。。。。