码迷,mamicode.com
首页 > Windows程序 > 详细

C#轻型ORM框架PetaPoco试水

时间:2016-11-01 00:45:47      阅读:232      评论:0      收藏:0      [点我收藏+]

标签:span   pre   initial   alt   功能   官方   partial   color   mod   

近端时间从推酷app上了解到C#轻微型的ORM框架--PetaPoco。从github Dapper 开源项目可以看到PetaPoco排第四

技术分享

以下是网友根据官方介绍翻译,这里贴出来。

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");
            }
        }
    }
}

 未完待续

C#轻型ORM框架PetaPoco试水

标签:span   pre   initial   alt   功能   官方   partial   color   mod   

原文地址:http://www.cnblogs.com/xiefengdaxia123/p/6017958.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!