好得很程序员自学网

<tfoot draggable='sEl'></tfoot>

ASP.NET中EF根据指定编号更新数据表中指定字段的值(EntityFramework6.0通过测

1.实体类:

public class HdhCmsStudent

{

 //自动编号

 public int Sid{get;set;}

 //学号

 public string SNo{get;set;}

 //手机号码

 public string SMobile{get;set;}

 //所在城市

 public string SCity{get;set;}

 //录入时间

 public datetime STime{get;set;}

}

2、更新表中指定字段的值

public void HdhCmsUpdate(HdhCmsStudent s)

{

//实例化数据库对象

//hdhcmsEntity 数据库上下文

 hdhcmsEntities hdhcmsEntity = new hdhcmsEntities();

 //定义对象接收数据表中返回的对象(FirstOrDefault 表示返回第一条数据或者返回默认值)

 HdhCmsStudent hdhcmsStudent = hdhcmsEntity.HdhCmsStudent.Where(a => a.sid == s.Sid).FirstOrDefault();

 //给字段重新值

            hdhcmsStudent.SName = s.SName;

            hdhcmsStudent.SNo = s.SNo;

            hdhcmsStudent.SMobile = s.SMobile;

            hdhcmsStudent.SCity = s.SCity;

 

 //调用方法写入数据库(返回受影响行数)

            hdhcmsEntity.SaveChanges();

}

3、更新表中所有字段的值

public void HdhCmsUpdate(HdhCmsStudent s)

{

            hdhcmsEntity.Entry(s).State = EntityState.Modified;

            hdhcmsEntity.SaveChanges();


}


查看更多关于ASP.NET中EF根据指定编号更新数据表中指定字段的值(EntityFramework6.0通过测的详细内容...

  阅读:65次