예제 #1
0
 protected void Page_Load(object sender, EventArgs e)
 {
     if (!IsPostBack)
     {
         if (Request.QueryString["type"] == "add")
         {
             string strName = Request.QueryString["Name"];
             string strAge = Request.QueryString["Age"];
             string strSex = Request.QueryString["Sex"];
             using (StudentEntities ent = new StudentEntities())
             {
                 User aNewUser = new User()
                 {
                     Age = 20,
                     Name = strName,
                     Sex = strSex
                 };
                 ent.User.Add(aNewUser);
                 if (ent.SaveChanges() > 0)
                 {
                     Response.Write("添加成功。");
                 }
                 else
                 {
                     Response.Write("添加失败。");
                 }
                 Response.End();
             }
         }
     }
 }
예제 #2
0
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (Request.QueryString["type"] == "loadData")
                {
                    using (StudentEntities ent = new StudentEntities())
                    {
                        List<User> userList = ent.User.ToList();
                        JavaScriptSerializer js = new JavaScriptSerializer();
                        string strJsonData = js.Serialize(userList);
                        Response.Write(strJsonData);
                        Response.End();
                    }
                }
                if (Request.QueryString["type"] == "del")
                {
                    string strID = Request.QueryString["id"];
                    int iID = int.Parse(strID);
                    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() > 0)
                            {
                                Response.Write("删除成功。");
                            }
                            else
                            {
                                Response.Write("删除失败。");
                            }
                        }
                        else
                        {
                            Response.Write("不存在此用户,请确认用户ID是否正确。");
                        }
                        Response.End();
                    }
                }
            }
        }