PetaPoco是一款適用於.Net 和Mono的微小、快速、單文件的微型ORM。git
PetaPoco有如下特點:github
// Create a PetaPoco database object var db=new PetaPoco.Database("connectionStringName"); // Show all articles foreach (var a in db.Query<article>("SELECT * FROM articles")) { Console.WriteLine("{0} - {1}", a.article_id, a.title); }
long count=db.ExecuteScalar<long>("SELECT Count(*) FROM articles");
var a = db.SingleOrDefault<article>("SELECT * FROM articles WHERE article_id=@0", 123));
var result=db.Page<article>(1, 20, // <-- page number and items per page "SELECT * FROM articles WHERE category=@0 ORDER BY date_posted DESC", "coolstuff");
1.生成並執行一個查詢,獲取匹配的數據行數。web
2.修改原始的查詢語句,只能獲得全部匹配的一個子集。數據庫
pageFetch對象:一個展現單頁數據和分頁控制的類。app
public class Page<T> where T:new() { public long CurrentPage { get; set; } public long ItemsPerPage { get; set; } public long TotalPages { get; set; } public long TotalItems { get; set; } public List<T> Items { get; set; } }
Fetch返回一個一個POCO類的List<>,而Query使用迭代全部數據,可是這些數據沒有被加載到內存中。 ide
db.Execute("DELETE FROM articles WHERE draft<>0");
// Create the article var a=new article(); a.title="My new article"; a.content="PetaPoco was here"; a.date_created=DateTime.UtcNow; // Insert it db.Insert("articles", "article_id", a); // by now a.article_id will have the id of the new article
// Get a record var a=db.SingleOrDefault<article>("SELECT * FROM articles WHERE article_id=@0", 123); // Change it a.content="PetaPoco was here again"; // Save it db.Update("articles", "article_id", a);
db.Update("articles", "article_id", new { title="New title" }, 123);
// 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);
// Represents a record in the "articles" table [PetaPoco.TableName("articles")] [PetaPoco.PrimaryKey("article_id")] public class article { public long article_id { get; set; } public string title { get; set; } public DateTime date_created { get; set; } public bool draft { get; set; } public string content { get; set; } }
// Insert a record var a=new article(); a.title="My new article"; a.content="PetaPoco was here"; a.date_created=DateTime.UtcNow; db.Insert(a); // Update it a.content="Blah blah"; db.Update(a); // Delete it db.Delete(a);
// Delete an article db.Delete<article>("WHERE article_id=@0", 123); // Update an article db.Update<article>("SET title=@0 WHERE article_id=@1", "New Title", 123);
public class article { [PetaPoco.Ignore] public long SomeCalculatedFieldPerhaps { get; set; } }
// 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;} }
運行一個不以select開頭的查詢, PetaPoco會自動的將它加上。post
IsNew:檢測是否爲新增。性能
Save:根據判斷的結果執行Insert或Update。單元測試
using (var scope=db.Transaction) { // Do transacted updates here // Commit scope.Complete(); }
注意:爲了使用事務,全部操做都須要相同的PetaPoco Database對象實例。測試