惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

T
Tenable Blog
Last Week in AI
Last Week in AI
P
Proofpoint News Feed
Engineering at Meta
Engineering at Meta
H
Help Net Security
F
Fortinet All Blogs
MyScale Blog
MyScale Blog
宝玉的分享
宝玉的分享
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
博客园 - 司徒正美
量子位
N
Netflix TechBlog - Medium
Apple Machine Learning Research
Apple Machine Learning Research
小众软件
小众软件
Recorded Future
Recorded Future
博客园 - 三生石上(FineUI控件)
Vercel News
Vercel News
aimingoo的专栏
aimingoo的专栏
I
InfoQ
Microsoft Security Blog
Microsoft Security Blog
Scott Helme
Scott Helme
The Last Watchdog
The Last Watchdog
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
IT之家
IT之家
AI
AI
WordPress大学
WordPress大学
Security Archives - TechRepublic
Security Archives - TechRepublic
Google Online Security Blog
Google Online Security Blog
U
Unit 42
V2EX - 技术
V2EX - 技术
MongoDB | Blog
MongoDB | Blog
Schneier on Security
Schneier on Security
博客园 - Franky
H
Heimdal Security Blog
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
Jina AI
Jina AI
W
WeLiveSecurity
P
Privacy & Cybersecurity Law Blog
Cloudbric
Cloudbric
B
Blog RSS Feed
N
News | PayPal Newsroom
S
Securelist
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
I
Intezer
Hacker News - Newest:
Hacker News - Newest: "LLM"
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
博客园_首页
罗磊的独立博客
H
Hackread – Cybersecurity News, Data Breaches, AI and More
雷峰网
雷峰网

博客园 - 我想我是青蛙

将golang程序注册为windows服务 CentOS上安装spark standalone mode(转载) 关于听歌这回事 c#读取apk 信息 golang 读取mongob数据写入sqlserver golang 通用Contains方法 golang读取文件信息插入mongodb 白话MongoDB(三)(转载) 白话MongoDB(二)(转载) 白话MongoDB(一) (转载) 给文章加入关键字链接 针对firefox ie6 ie7 ie8的css样式hack (转载) 好久不写日志了,现在开始,好好写了。。 sharepoint 查询calendar recurrence sharepoint之lookup字段 sharepoint获取Audiences 获取exchangeserve的calendar的item sharepoint错误处理 c#上传下载ftp(支持断点续传)
PetaPoco介绍
我想我是青蛙 · 2012-02-28 · via 博客园 - 我想我是青蛙

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

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