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

推荐订阅源

aimingoo的专栏
aimingoo的专栏
V
V2EX
G
Google Developers Blog
F
Full Disclosure
Martin Fowler
Martin Fowler
宝玉的分享
宝玉的分享
H
Hacker News: Front Page
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
NISL@THU
NISL@THU
G
GRAHAM CLULEY
V
Vulnerabilities – Threatpost
Hacker News - Newest:
Hacker News - Newest: "LLM"
A
About on SuperTechFans
The Cloudflare Blog
C
Cisco Blogs
D
DataBreaches.Net
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
Vercel News
Vercel News
P
Privacy International News Feed
Microsoft Security Blog
Microsoft Security Blog
Help Net Security
Help Net Security
Recorded Future
Recorded Future
PCI Perspectives
PCI Perspectives
S
Schneier on Security
AI
AI
N
News | PayPal Newsroom
雷峰网
雷峰网
C
Cyber Attacks, Cyber Crime and Cyber Security
P
Proofpoint News Feed
The Last Watchdog
The Last Watchdog
L
LINUX DO - 最新话题
Hugging Face - Blog
Hugging Face - Blog
Apple Machine Learning Research
Apple Machine Learning Research
Schneier on Security
Schneier on Security
S
Securelist
云风的 BLOG
云风的 BLOG
Stack Overflow Blog
Stack Overflow Blog
博客园_首页
AWS News Blog
AWS News Blog
TaoSecurity Blog
TaoSecurity Blog
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
Recent Commits to openclaw:main
Recent Commits to openclaw:main
博客园 - 三生石上(FineUI控件)
C
CXSECURITY Database RSS Feed - CXSecurity.com
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
Cloudbric
Cloudbric
C
Cybersecurity and Infrastructure Security Agency CISA
Project Zero
Project Zero
C
Check Point Blog
S
Security Affairs

博客园 - 我想我是青蛙

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

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