接着上一篇,上一篇主要介绍了petapoco 基本情况,优缺点和基本的查询功能,所以这篇主要介绍的是petapoco
的增,删,改等功能。petapoco提供了完整的增,删,改,查功能。是代替sqlhelper辅助类的不二选择。
db.insert("articles",
"article_id", a);
如果是t4模板自动生存的poco 对象,直接 a.insert() 即可 。
更新一条数据或是更新某个字段:
a.content="balah balah";
db.update(a);
删除
// delete an article extracting the primary key from a record
db.delete("articles", "article_id", a);
// or if you already have the id elsewhere
db.delete("articles", "article_id", null, 123);
定义poco类,或者通过t4模板生成,这样增删改查会更加简单:
// represents a record in the "articles" table
[petapoco.tablename("articles")]
[petapoco.primarykey("article_id")]
[petapoco.explicitcolumns]
public class article
{
[petapoco.column]publiclong article_id { get; set;}
[petapoco.column]publicstring title { get; set;}
[petapoco.column]publicdatetime date_created { get;
set;}
[petapoco.column]public bool draft { get; set;}
[petapoco.column]publicstring content { get; set;}
}
增加
var a=new article();
a.title="my new article";
a.content="petapoco was here";
a.date_created=datetime.utcnow;
db.insert(a);
修改
a.content="blah blah";
删除对象
db.delete(a);
there are also other overloads for update and delete:
删除某条或多条记录
db.delete<article>("where article_id=@0", 123);
修改一个对象的单独几个字段:
db.update<article>("set title=@0 where article_id=@1", "new title",
123);
同时,你可以告诉petapoco 忽略某个字段
[petapoco.ignore]
public long somecalculatedfieldperhaps
{
get; set;
}