public bool Update(Quest quest)
{
try
{
PromotionDataClassesDataContext db = new PromotionDataClassesDataContext(connstr);
var U = (from u in db.Quest where u.QuestID == quest.QuestID select u).First();
U=quest;
db.SubmitChanges();
return true;
}
catch
{
return false;
}
}
原本以为这样写是没有问题的,(红字部分),但是却更新不成功。
所以用以下办法解决,使用放射技术变量类中所有属性。
public bool Update(Quest quest)
{
try
{
PromotionDataClassesDataContext db = new PromotionDataClassesDataContext(connstr);
var U = (from u in db.Quest where u.QuestID == quest.QuestID select u).First();
Type t = U.GetType();
foreach (PropertyInfo pi in t.GetProperties())
{
object value = t.GetProperty(pi.Name).GetValue(quest, null);//获取quest中的属性值
t.GetProperty(pi.Name).SetValue(U, value, null);//设置U中的属性值
}
db.SubmitChanges();
return true;
}
catch
{
return false;
}
}