asp.net mvc 3 linq实现数据的增、删、改、查、
添加数据
定义一个对象:
public class Student
{
public int id{get; set;}
public string Name{get;set;}
public string Age{get;set;}
}
将其添加到上下文中:
public DbSet<Student> student{get;set;}
增加一条数据:
要添加一个上下文比如就叫 private StudentEntities db = new StudentEntities ();
Student stu=new Student();
stu.name=name;
stu.Age=age;
db.student.Add(stu);
db.SaveChanges();
删除数据:
Student stu =db.student.Where(p => p.id == id).SingleOrDefault();
db.student.Remove(stu);
db.SaveChanges();
修改数据:
Student stu =db.student.Where(p => p.id == id).SingleOrDefault();
stu.name=name;
stu.Age=age;
db.Entry(stu).State = System.Data.EntityState.Modified;
db.SaveChanges();
查询数据 :
var stu=from s in db.student where s.id=id from selelct;