using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DAL;
using SqlSugar;//注意引入命名空间
namespace Demo
{
public partial class Demo : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
//详细教程 http://www.cnblogs.com/sunkaixuan/p/4649904.html
using (var db = SugarDao.GetInstance())
{
var list= db.Queryable<Model.Student>().ToList();//查询所有
GridView1.DataSource = list;
GridView1.DataBind();
//取10-20条
var page1 = db.Queryable<Model.Student>().Where(c => c.id > 9).OrderBy("id").Skip(1).Take(10).ToList();
//Skip 跳过,takbe 拿
GridView2.DataSource = page1;
GridView2.DataBind();
//取10-20条
var page2 = db.Queryable<Model.Student>().Where(c => c.id > 9).OrderBy("id").Skip(1).Take(10).ToList();
GridView2.DataSource = page2;//数据源
GridView2.DataBind();//绑定数据
//取20条数据的简化写法
var page3 = db.Queryable<Model.Student>().Where(c => c.id > 1).OrderBy("id").ToPageList(2,10);
GridView3.DataSource = page3;//数据源
GridView3.DataBind();//绑定数据
//查询条数
var count = db.Queryable<Model.Student>().Where(c => c.id > 10).Count();
Console.WriteLine(count);
//从第二条之后取所有的
var skipw = db.Queryable<Model.Student>().Where(c => c.id > 1).OrderBy("id").Skip(2).ToList();
GridView4.DataSource = skipw;
GridView4.DataBind();
//取出前两条数据
var take = db.Queryable<Model.Student>().Where(c => c.id > 1).OrderBy("id").Take(2).ToList();
GridView5.DataSource = take;
GridView5.DataBind();
//not like
var like = db.Queryable<Model.Student>().Where(c => !c.name.Contains("绿牛".ToString())).ToList();
GridView6.DataSource = like;
GridView6.DataBind();
//可以在lambda中使用tostring 和convert
var convert1 = db.Queryable<Model.Student>().Where(c => c.name == "绿牛".ToString()).ToList();
var convert2 = db.Queryable<Model.Student>().Where(c => c.id == Convert.ToInt32(1)).ToList();
var convert3 = db.Queryable<Model.Student>().Where(c => DateTime.Now < Convert.ToDateTime("2019-4-1")).ToList();
GridView7.DataSource = convert3;
GridView7.DataBind();
//支持字符串Where 让你解决,更复杂的查询
var student12 = db.Queryable<Model.Student>().Where(c => 1 == 1).Where("id>@id", new { id = 1 }).ToList();
//var student12 = db.Queryable<Model.Student>().Where(c => 1 == 1).Where("id>@id", new { id = 1 }).ToList();
GridView8.DataSource = student12;
GridView8.DataBind();
}
}
}
}