protected void Button1_Click(object sender, EventArgs e) { //查询部分列 //通过在select中加入匿名类 StudyEntities db = null; if (HttpContext.Current.Items["db"] == null) { db = new StudyEntities(); HttpContext.Current.Items["db"] = db; } else { db = HttpContext.Current.Items["db"] as StudyEntities; } var userInfoList = from u in db.UserInfo where u.UserId == 1039 select new { UName = u.UserName, UPwd = u.UserPwd }; foreach (var item in userInfoList) { Response.Write(item.UName); //Response.Write(UserInfo.) } }
/// <summary> /// 删除 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> protected void Button4_Click(object sender, EventArgs e) { StudyEntities studyEntities = new StudyEntities(); //var userInfoList = from u in studyEntities.UserInfo // where u.UserId == 1166 // select u; //UserInfo userInfo = userInfoList.FirstOrDefault();//返回第一个元素,如果没有则返回NULL //if (userInfo != null) //{ // //studyEntities.UserInfo.Remove(userInfo); // studyEntities.Entry<UserInfo>(userInfo).State = EntityState.Deleted; // studyEntities.SaveChanges(); //} //else //{ // Response.Write("删除的数据不存在"); //} //若要用Remove 则UserInfo对象需从EF中获取才可用; //而通过设置State的方式则不需要如此; UserInfo userInfo = new UserInfo() { UserId = 1162 }; //studyEntities.UserInfo.Remove(userInfo); studyEntities.Entry <UserInfo>(userInfo).State = EntityState.Deleted; studyEntities.SaveChanges(); }
protected void Button4_Click(object sender, EventArgs e) { StudyEntities db = new StudyEntities(); var userInfoList = db.UserInfo.Where(u => u.UserId > 0).ToList(); int i = 0; int count = userInfoList.Count(); Response.Write(count); }
/// <summary> /// 更新 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> protected void Button5_Click(object sender, EventArgs e) { StudyEntities db = new StudyEntities(); var userInfoList = from u in db.UserInfo where u.UserId == 1163 select u; var userInfo = userInfoList.FirstOrDefault(); userInfo.UserPwd = "666666"; db.Entry <UserInfo>(userInfo).State = EntityState.Modified; db.SaveChanges(); }
protected void Button2_Click(object sender, EventArgs e) { //查询 StudyEntities studyEntities = new StudyEntities(); //linq表达式 var userInfoList = from u in studyEntities.UserInfo where u.UserId == 1166 select u; int i = 0; //延迟加载机制 数据使用到的时候吗,才会进行查询,否则不会进行查询 foreach (var item in userInfoList) { Response.Write(item.UserName); } }
/// <summary> /// 分页排序查询 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> protected void Button2_Click(object sender, EventArgs e) { StudyEntities db = new StudyEntities(); int pageIndex = 2; int pageSize = 2; //排序OrderBy 分页 Skip(跳过几条数据)Take(取出几条数据) var userInfoList = db.UserInfo.Where(t => t.UserId > 0).OrderByDescending(u => u.UserId).ThenByDescending(t => t.UserPwd).Skip((pageIndex - 1) * pageIndex).Take(pageSize); //var userInfoList = (from u in db.UserInfo // where u.UserId > 0 // orderby u.UserId ascending//descending降序 // select u).Skip((pageIndex - 1) * pageIndex).Take(pageSize);//分页与lambda表达式一样的处理 foreach (var item in userInfoList) { Response.Write(item.UserName); } }
protected void Button1_Click(object sender, EventArgs e) { //新增 UserInfo userInfo = new UserInfo(); userInfo.UserName = "******"; userInfo.UserPwd = "123456"; userInfo.RegDate = DateTime.Now; StudyEntities db = new StudyEntities(); //将数据添加到EF 并且添加了添加标记(Add) db.UserInfo.Add(userInfo); //更新到数据库 //返回受影响的行数 db.SaveChanges(); Response.Write(userInfo.UserId); }