用php做的网站源代码,哈铁工程建设公司网站,做网站难学吗,北京展厅设计公司.net的轻量级ORM -- PetaPoco/NPOCO框架使用说明(具体参看:http://www.toptensoftware.com/petapoco/) 从11年就开始尝试使用轻量级ORM#xff1a;PetaPoco,下文是基本使用方法。另外NPoco是PetaPoco的升级版#xff0c;是另外一个人维护#xff0c;原版PetaPoco基本不再维护….net的轻量级ORM -- PetaPoco/NPOCO框架使用说明(具体参看:http://www.toptensoftware.com/petapoco/) 从11年就开始尝试使用轻量级ORMPetaPoco,下文是基本使用方法。另外NPoco是PetaPoco的升级版是另外一个人维护原版PetaPoco基本不再维护。NPoco大多数用法和PetaPoco一致另外有些额外的功能。NPoco我会考虑再写一篇文章介绍。 运行查询 首先定义POCO 注:POCO意思是Plain Old CLR Object即指一般指带有无参构造函数只有get set的简单.net类: // 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; }
} 查询 // Create a PetaPoco database object
var dbnew PetaPoco.Database(connectionStringName);// Show all articles
foreach (var a in db.Queryarticle(SELECT * FROM articles))
{
Console.WriteLine({0} - {1}, a.article_id, a.title);
} 注意: Database有Fetch和Query两个方法 Fetch返回ListT.而Query通过yield return 返回,使得不用遍历记录不用通过转载整个数据到内存里面. 注意:少量数据用Fetch更方便,大量数据而且只是单向遍历或者返回请用Query,以节约内存. 返回单个值 long countdb.ExecuteScalarlong(SELECT Count(*) FROM articles); 返回单条记录 var a db.SingleOrDefaultarticle(SELECT * FROM articles WHERE article_id0, 123)); 注意:当运行SingleOrDefault返回超过1条记录会报错 分页查询 注意:所有大数据量查询请都使用这个分页的方法,PetaPoco的分页查询是数据库分页. var resultdb.Pagearticle(1, 20, // -- page number and items per page
SELECT * FROM articles WHERE category0 ORDER BY date_posted DESC, coolstuff); 会返回 public class PageT where T:new()
{public long CurrentPage { get; set; }public long ItemsPerPage { get; set; }public long TotalPages { get; set; }public long TotalItems { get; set; }public ListT Items { get; set; }
} 运行分页方法,实际上PetaPoco会执行两件事: 返回匹配的所有总记录数.得到需要的页数的字记录数. 注意:PetaPoco会把你的sql转换成分页sql,所以对sql有一定限制,请不要用select * 而且写得sql严格用空格分开,避免PetaPoco不能正确地解析你的sql. 新增,更新,删除记录新增 // Create the article
var anew article();
a.titleMy new article;
a.contentPetaPoco was here;
a.date_createdDateTime.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 adb.SingleOrDefaultarticle(SELECT * FROM articles WHERE article_id0, 123);
// Change it
a.contentPetaPoco was here again;
// Save it
db.Update(articles, article_id, a);可以用匿名对象更新,以下是仅更新title的例子
db.Update(articles, article_id, new { titleNew title }, 123); 装饰你的Poco // 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 anew article();
a.titleMy new article;
a.contentPetaPoco was here;
a.date_createdDateTime.UtcNow;
db.Insert(a);// Update it
a.contentBlah blah;
db.Update(a);// Delete it
db.Delete(a); 当然也可以这样运行 // Delete an article
db.Deletearticle(WHERE article_id0, 123);// Update an article
db.Updatearticle(SET title0 WHERE article_id1, New Title, 123); 也可以忽略某些属性 public class article
{[PetaPoco.Ignore]public long SomeCalculatedFieldPerhaps{ get; set; }
} 使用事务 using (var trans db.getTransaction())
{// Do transacted updates here// Committrans.Complete();
} SQL Builder var id123;
var sqlPetaPoco.Sql.Builder.Append(SELECT * FROM articles).Append(WHERE article_id0, id);if (start_date.HasValue)sql.Append(AND date_created0, start_date.Value);if (end_date.HasValue)sql.Append(AND date_created0, end_date.Value);var adb.Queryarticle(sql); 也可以使用名字命名: sql.Append(AND date_createdstart AND date_createdend, new { startDateTime.UtcNow.AddDays(-2), endDateTime.UtcNow };
); 也可以这样使用 var sqlPetaPoco.Sql.Builder().Select(*).From(articles).Where(date_created 0, DateTime.UtcNow).OrderBy(date_created DESC); sql使用where in 语法可以这样 var tagsToFind new string[] { SqlServer, IIS };
var sql PetaPoco.Sql.Builder.Select(*).From(Tags).Where(Name in (tags), new { tags tagsToFind });
var result db.QueryTag(sql); 生成的sql如下 select * from Tags where name in (0, 1);
0 SqlServer, 1 IIS 转载于:https://www.cnblogs.com/shiningplus/p/6848991.html