以下是网友根据官方介绍翻译,这里贴出来。
PetaPoco是一款适用于.Net 和Mono的微小、快速、单文件的微型ORM。
PetaPoco有以下特色:
微小,没有依赖项……单个的C#文件可以方便的添加到任何项目中。
工作于严格的没有装饰的Poco类,和几乎全部加了特性的Poco类
Insert/Delete/Update/Save and IsNew 等帮助方法。
分页支持:自动得到总行数和数据
支持简单的事务
更好的支持参数替换,包括从对象属性中抓取命名的参数。
很好的性能,,剔除了Linq,并通过Dynamic方法快速的为属性赋值
T4模板自动生成Poco类
查询语言是Sql……不支持别扭的fluent或Linq语法(仁者见仁,智者见智)
包含一个低耦合的Sql Builder类,让内联的Sql更容易书写
为异常信息记录、值转换器安装和数据映射提供钩子。(Hooks for logging exceptions, installing value converters and mapping columns to properties without attributes.)
兼容SQL Server, SQL Server CE, MySQL, PostgreSQL and Oracle。
可以在.NET 3.5 或Mono 2.6或更高版本上运行
在.NET 4.0 和Mono 2.8下支持dynamic
NUnit单元测试
开源(Apache License)
所有功能大约用了1500行代码
如何获取PetaPoco?
因为中国使用win7系统的比较多,然后win7自带.net3.5框架,所以笔者从nuget下载了4.0.3版本的PetaPoco
获取地址:
可以和笔者一样安装4.0.3版本
如下图,回车即可
下面贴出一部分测试代码
using PetaPocoDemo.Models; using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; namespace PetaPocoDemo { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { //创建一个petapoco对象 var db = new PetaPoco.Database("connectionString"); //遍历查询文章表 foreach (var a in db.Query<article>("select * from article")) { MessageBox.Show(a.article_id + "-------" + a.title); } //返回一个scalar数量 var count = db.ExecuteScalar<int>("select count(*) from article"); MessageBox.Show("count:" + count.ToString()); //返回一行记录 var row = db.SingleOrDefault<article>("select * from article where article_id=‘1‘"); MessageBox.Show(row.content); //插入记录 var newArticle = new article(); newArticle.article_id = 2; newArticle.title = "绿书"; newArticle.content = "可持续发展绿色内容"; newArticle.date_created = DateTime.UtcNow; if (Convert.ToInt32(db.Insert("article", "article_id",false, newArticle)) > 0) { MessageBox.Show("sucess"); } else { MessageBox.Show("fail"); } } } }