接着上一篇,上一篇主要介紹了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;
}