



























Petapoco一
PetaPoco是一个微小的,快速的,单个文件的微型ORM,可以运行在.NET和Mono平台上。
特性:
简单介绍下用法
首先,定义一个POCO类
View Code
// Represents a record in the "articles" table
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; }
}
下一步,创建一个PetaPoco.Database 并且运行查询。
View Code
// 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);
}
查询 scalar:
View Code
long count=db.ExecuteScalar<long>("SELECT Count(*) FROM articles");
获取单个记录
View Code
分页
View Code
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");
会返回一个Page对象:
View Code
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; }
}
PetaPoco 有一些帮助为insert, update 和 delete 操作.
可以有几种不同的方式插入,先介绍最简单的一种:
View Code
// 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(a);// by now a.article_id will have the id of the new article
更新
View Code
// 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(a);
删除
View Code
db.Delete("articles", "article_id", null, 123);
PetaPoco可以获取多个实体,并且petapoco无侵入性。譬如
可以获取一堆一,一对多,多对多的数据集。现在在项目中使用,十分方便,基本满足了要求。同时也对其进行了扩展,后续会说到。
此内容由惯性聚合(RSS阅读器)自动聚合整理,仅供阅读参考。 原文来自 — 版权归原作者所有。