一、 EF分页查询
1 #region EF分页 2 static void UserPagingQuery (int page, int pageSize) { 3 var p = page == 0 ? 0 : (page - 1) * pageSize; 4 DemoDbEntities db = new DemoDbEntities (); 5 //在分页前先要是用OrderBy或者OrderByDescending对数据进行正序或者倒序 6 //然后在skip()跳过多少条,take()查询多少条。 7 db.User.OrderBy (u => u.ID) 8 .Skip (p) 9 .Take (pageSize) 10 .ToList () 11 .ForEach (c => Console.WriteLine (c.ID)); 12 } 13 #endregion
二、EF中执行SQL
1 #region EF中执行SQL语句 2 3 static void EFExecuteSql () { 4 DemoDbEntities db = new DemoDbEntities (); 5 string sql = "update [DemoDb].[dbo].[User] set NAME=@name where ID>@id"; 6 7 SqlParameter[] p = new SqlParameter[] { 8 new SqlParameter ("@id", 5), 9 new SqlParameter ("@name", "王五") 10 }; 11 db.Database.ExecuteSqlCommand (sql, p); 12 Console.WriteLine ("修改成功"); 13 } 14 15 #endregion