1.通过PetaPoco.Sql.Builder来构建Sql语句,构建Sql语句时采用连缀的方式,例如:
var sql = PetaPoco.Sql.Builder
.Select("id")
.From("student");
return sql;
var sql = PetaPoco.Sql.Builder;
sql.Where(" id=@0 or name=@1", id, name);
return sql;
var sql = PetaPoco.Sql.Builder;
sql.Select("id")
.From("student")
.Where("id=@0",id);
return sql;
4.通过OrderBy方法来添加排序条件,如果有多个排序条件时则需要多个.OrderBy方法来添加,例如:
var sql = PetaPoco.Sql.Builder;
sql.Select("id")
.From("student")
.Where("id=@0",id)
.OrderBy("Id desc")
.OrderBy("name desc");
return sql;
5.通过Append方法来手动设置整个Sql语句,例如:
var sql = PetaPoco.Sql.Builder;
sql.Append(" select * form student where id=@0",id)
.Append(" and name=@1",name);
return sql;
var sql = PetaPoco.Sql.Builder;
if(query.Id.HasValue){
sql.where("Id=@0",query.Id.Value);
}
sql.OrderBy("name asc");
return sql;
7. 如果构造Select语句则必须同时构造From语句,但是允许构造From语句而不构造Select语句。 可以通过指定占位符的方式添加sql语句的参数,例如:
var sql = PetaPoco.Sql.Builder;
sql.Append(" update student set name=@0",name);
string strIds= string.Empty;
sql.where("Id=@commentId",new {commentId=id});
Database.Execute(sql);
var sql = PetaPoco.Sql.Builder;
sql.Append(" update student set name=@0",name);
string strIds= "2,33,454";
sql.where("Id in @ids",new {ids=strIds});
Database.Execute(sql);
IList sqls = new List();
foreach(string name in names)
{
sqls.Add(Sql.Builder.Append(" insert into student (name,address,age)values (@name,@address,@age)", name,address,age))
}
int affectCount = Database.Execute(sqls);