zoukankan      html  css  js  c++  java
  • PetaPoco介绍

    Petapoco一

    PetaPoco是一个微小的,快速的,单个文件的微型ORM,可以运行在.NET和Mono平台上。

    特性:

    • 微小的,没有依赖…单个文件,可以容易的添加进任何项目
    • 可以与严格的简单的POCOS对象工作或者有特性标记的POCOS
    • 帮助方法:Inert/Delete/Update/Save 和 IsNew
    • 内嵌分页方法
    • 事物支持
    • 良好的性能
    • 包含T4模板自动产生POCO类
    • 使用Sql查询而不是怪异的Linq语法(汗一个)
    • 包含一个SQL Builder类产生Sql更加容易
    • 兼容SQL Server,SQL Server CE,MySql, PostgreSQL and Oracle.
    • 可以再.NET 3.5或者Mono 2.6 以上版本使用
    • 支持动态在.NET4.0和Mono 2.8
    • 开源(Apache License)

    简单介绍下用法

    首先,定义一个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; }
    }


    Inserts, Updates and Deletes

    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无侵入性。譬如

    可以获取一堆一,一对多,多对多的数据集。现在在项目中使用,十分方便,基本满足了要求。同时也对其进行了扩展,后续会说到。

    关于PetaPoco先介绍到这里,有兴趣的可以去http://www.toptensoftware.com/petapoco/ 这个网站去了解,我后面争取多写和翻译一些关于petapoco的文章。深入其内部机制。






  • 相关阅读:
    配置Mysql远程访问
    mysql Access denied for user root@localhost错误处理备忘
    MsSql判断表是否有自增标识
    .net用NPOI生成Word表格
    TensorFlow-卷积
    Java中int与Integer的区别
    由经纬度坐标得到腾讯地图的瓦片/切片行列号
    vs code上配置Scala
    VMware虚拟机上配置CentOS联网
    使用PuTTy在CentOS下安装web.py与简单的文件传输
  • 原文地址:https://www.cnblogs.com/zsbfree/p/2371737.html
Copyright © 2011-2022 走看看