首先要添加一个ADO.NET实体数据模型
添加一个Entities 对象,其用法和linqtosql类似
例如:
StudentInfoEntities2 entity = new StudentInfoEntities2();
protected void Page_Load(object sender, EventArgs e)
{
if(!Page.IsPostBack)
{
studentIfo();
}
}
protected void Button1_Click(object sender, EventArgs e)
{//添加用户
T_Stu newUser = new T_Stu { ID = "100112006", Sname = "马磊", Sex = "女", Sage = 20 };
entity.AddToT_Stu(newUser);//添加新用户信息
entity.SaveChanges();//更新数据库
studentIfo();
}
protected void Button2_Click(object sender, EventArgs e)
{//更新用户信息
var upuser=(from userup in entity.T_Stu
where userup.ID=="100112006"
select userup
).First();
upuser.Sage = 19;
entity.SaveChanges();
studentIfo();
}
protected void Button3_Click(object sender, EventArgs e)
{//删除用户
var upuser = (from userup in entity.T_Stu
where userup.ID == "100112006"
select userup
).First();
entity.DeleteObject(upuser);
entity.SaveChanges();
studentIfo();
}
void studentIfo()
{
var userList = from user in entity.T_Stu
select user;
GridView1.DataSource = userList;
GridView1.DataBind();
}