王胜龙的博客
OrmLite-查询1

根据id查询一条数据

var person=db.SingleById<Person>(1); //or var person=db.SingleById<Person>(1,"_id");

根据条件查询一条数据

var person=db.Single<Person>("age>10 and sex=1"); //select * from person where age>10 and sex=1 var person=db.Single<Person>("age>10 and Name=@name", DictBuilder.Assign("name", "abc")); //select * from person where age>10 and Name=@name; @name='abc' var person=db.Single<Person>("select ID,Name from Person where age>10 and sex=1"); //原始sql var person=db.Single<Person>("select ID,Name from Person where age>10 and name=@name", DictBuilder.Assign("name", "abc")); //原始sql带参数

参数化查询语句

var person=db.SingleFmt<Person>("select ID,Name from Person where age>{0} and sex={1}",10,1);

根据单个查询条件查询单条数据

var person= db.SingleWhere<Person>("id",23);

使用字典条件查询单条数据

var person= db.SingleWhere<Person>(DictBuilder.Assign("name","loogn").Assign("age",23));

使用匿名对象作为条件查询单条数据

var person= db.SingleWhere<Person>(new{ Name = "loogn", Age=23});

查询全部

var list=db.Select<Person>();

in查询

var list=db.SelectByIds<Person>(new int[] { 1, 2, 3 }); //select * from person where id in (1,2,3); var list=db.SelectByIds<Person>(new string[] { "1", "2", "3" },"userId"); //select * from person where userId in ("1","2","3"); var list=db.SelectByIds<Person>(new string[] { "1", "2", "3" },"userId","id,name"); //select id,name from person where userId in ("1","2","3");

返回多个结果集

var cmds = new MutipleCmd[] { new MutipleCmd{ CmdText="select * from person"}, new MutipleCmd{ CmdText="select count(0) from person where id=@id",Params=DictBuilder.Assign("id",23)}, new MutipleCmd{ CmdText="select top 1 * from User where age>23"}, }; using (var fetcher = db.SelectMutipleResult(cmds)) { var personList = fetcher.FetchList<Person>(); //对应第一个命令 var personCount = fetcher.FetchScalar<int>(); //对应第二个命令 var user = fetcher.FetchObject<User>(); //对应第三个命令 }

其他Select和上面Single方法类似

分页查询

var pageResult = db.SelectPage<Person>(new OrmLitePageFactor { Conditions = "id>2 and age=@age", Fields = "ID,Name", OrderBy = "ID desc", PageIndex = 1, PageSize = 10, Params = DictBuilder.Assign("age", 23) }); //也可以连接查询 var pageResult = db.SelectPage<Person>(new OrmLitePageFactor { Conditions = "p.id>2 and p.age=@age", Fields = "p.*,t.Name as TypeName", OrderBy = "p.ID desc", PageIndex = 1, PageSize = 10, Params = DictBuilder.Assign("age", 23), TableName="Person p inner join Type t on p.Typeid=t.id" });
上一篇     目录     下一篇