标签:linq line 信息 complete package 添加 生成 .config write
PetaPoco是一款适用于.Net 和Mono的微小、快速、单文件的微型ORM。
PetaPoco有以下特色:
// 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); }
long count=db.ExecuteScalar<long>("SELECT Count(*) FROM articles");
var a = db.SingleOrDefault<article>("SELECT * FROM articles WHERE article_id=@0", 123));
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");
1.生成并执行一个查询,获取匹配的数据行数。
2.修改原始的查询语句,只能得到所有匹配的一个子集。
pageFetch对象:一个展示单页数据和分页控制的类。
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; } }
Fetch返回一个一个POCO类的List<>,而Query使用迭代所有数据,但是这些数据没有被加载到内存中。
db.Execute("DELETE FROM articles WHERE draft<>0");
// 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("articles", "article_id", a); // by now a.article_id will have the id of the new article
// 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("articles", "article_id", a);
db.Update("articles", "article_id", new { title="New title" }, 123);
// Delete an article extracting the primary key from a record db.Delete("articles", "article_id", a); // Or if you already have the ID elsewhere db.Delete("articles", "article_id", null, 123);
// Represents a record in the "articles" table [PetaPoco.TableName("articles")] [PetaPoco.PrimaryKey("article_id")] 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; } }
// Insert a record var a=new article(); a.title="My new article"; a.content="PetaPoco was here"; a.date_created=DateTime.UtcNow; db.Insert(a); // Update it a.content="Blah blah"; db.Update(a); // Delete it db.Delete(a);
// Delete an article db.Delete<article>("WHERE article_id=@0", 123); // Update an article db.Update<article>("SET title=@0 WHERE article_id=@1", "New Title", 123);
public class article { [PetaPoco.Ignore] public long SomeCalculatedFieldPerhaps { get; set; } }
// Represents a record in the "articles" table [PetaPoco.TableName("articles")] [PetaPoco.PrimaryKey("article_id")] [PetaPoco.ExplicitColumns] public class article { [PetaPoco.Column]publiclong article_id { get; set;} [PetaPoco.Column]publicstring title { get; set;} [PetaPoco.Column]publicDateTime date_created { get; set;} [PetaPoco.Column]public bool draft { get; set;} [PetaPoco.Column]publicstring content { get; set;} }
运行一个不以select开头的查询, PetaPoco会自动的将它加上。
IsNew:检测是否为新增。
Save:根据判断的结果执行Insert或Update。
using (var scope=db.Transaction) { // Do transacted updates here // Commit scope.Complete(); }
注意:为了使用事务,所有操作都需要相同的PetaPoco Database对象实例。
标签:linq line 信息 complete package 添加 生成 .config write
原文地址:http://www.cnblogs.com/mmry/p/6239120.html