using (StudentEntities ent = new StudentEntities())
{
User aNewUser = new User()
{
Age = ,
Name = strName,
Sex = strSex
};
ent.User.Add(aNewUser);
if (ent.SaveChanges() > )
{
Response.Write("添加成功。");
}
else
{
Response.Write("添加失败。");
}
Response.End();
}

 using (StudentEntities ent = new StudentEntities())
{
User aUser = (from c in ent.User where c.ID == iID select c).FirstOrDefault();
if (aUser != null)
{ ent.User.Remove(aUser);
if (ent.SaveChanges() > )
{
Response.Write("删除成功。");
}
else
{
Response.Write("删除失败。");
}
}
else
{
Response.Write("不存在此用户,请确认用户ID是否正确。");
}
Response.End();
}

改查

using (StudentEntities ent = new StudentEntities())
{
User aUser = (from c in ent.User where c.ID == iID select c).FirstOrDefault();
if (aUser != null)
{
aUser.Name = strName;
aUser.Age = ;
aUser.Sex = strSex;
if (ent.SaveChanges() > )
{
Response.Write("<script>alert('修改成功。')</script>");
}
}
}
05-07 15:15