PetaPoco 使用总结(二)
生活随笔
收集整理的這篇文章主要介紹了
PetaPoco 使用总结(二)
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
?
接著上一篇,上一篇主要介紹了PetaPoco 基本情況,優(yōu)缺點和基本的查詢功能,所以這篇主要介紹的是PetaPoco 的增,刪,改等功能。PetaPoco提供了完整的增,刪,改,查功能。是代替SqlHelper輔助類的不二選擇。
插入對象:需要指定的表和它的主鍵。
var a=new article();a.title="My new article"; a.content="PetaPoco was here"; a.date_created=DateTime.UtcNow;
db.Insert("articles", "article_id", a);
?
如果是T4模板自動生存的Poco 對象,直接? a.Insert() 即可 。
更新一條數(shù)據(jù)或是更新某個字段:
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.Update(a);?
?
刪除對象
db.Delete(a);?
刪除某條或多條記錄
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 class article {[PetaPoco.Ignore]public long SomeCalculatedFieldPerhaps{get; set;} }?
總結(jié)
以上是生活随笔為你收集整理的PetaPoco 使用总结(二)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: MDT2012部署问题,MDT中的驱动是
- 下一篇: hackerrank---Sets -