LINQ SubmitChanges 方法不更新数据库解决方法


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);

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

.net 程序开发 linq

无公害沙包 12 years, 7 months ago

你好 你的问题解决了吗 可能是你把Select函数定义成了 静态的函数 把static 去掉看下
我也不是很清楚 可能是static 那个HotelDataContext只会加载以前的 只有重新编译才能得到更新的内容。。。。。

仅仅如此 answered 12 years, 7 months ago

1.确认是否你的model和数据库的表不一样是否有过变动
2.确认你的数据库文件是不是在项目中被复制覆盖了
3.确认你的连接字符串,Try一下 submitchanges() 试试看

我曰曾轶可 answered 12 years, 7 months ago

代码上看是没有问题。。。

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


NC红领巾 answered 12 years, 7 months ago

可以这样

  C# code

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

萌化三仟院 answered 12 years, 7 months ago

你的数据库ID是不是有主健 没主健是不能UPate的

家具屋辉夜 answered 12 years, 7 months ago

<fieldset> <legend> 探讨 </legend>

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


快坏掉DA叔 answered 12 years, 7 months ago

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

猫耳型菜姬互浊 answered 12 years, 7 months ago

单步调试

普通废的柴 answered 12 years, 7 months ago

  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);

蛋疼的孩子 answered 12 years, 7 months ago

Your Answer