<?xml version="1.0" encoding="utf-8" ?> <configuration> <connectionStrings> <add name="connectionStringName" connectionString="server=localhost;database=0914;uid=sa;password=850414;" /> </connectionStrings> </configuration>
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace ConsoleApplication1 { public class Achievement { public int Id { get; set; } public string Name { get; set; } public string Course { get; set; } public int Degree { get; set; } } }
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace ConsoleApplication1 { class Program { static void Main(string[] args) { var db = new PetaPoco.Database("connectionStringName"); // Show all articles foreach (var a in db.Query<Achievement>("SELECT * FROM Achievement")) { Console.WriteLine("{0} - {1}", a.Id, a.Name); } //分页 Console.WriteLine("分页"); PetaPoco.Page<Achievement> result = db.Page<Achievement>(1, 3, // <-- page number and items per page "SELECT * FROM Achievement WHERE Course=@0 ORDER BY Degree DESC", "C#程序设计"); result.Items.ForEach(delegate(Achievement entity) { Console.WriteLine("PageItem:{0}-{1}-{2}-{3}",entity.Id,entity.Name,entity.Course,entity.Degree); }); Console.Read(); } } }
注意:
这里的分页执行的语句是:
SELECT * FROM
(SELECT ROW_NUMBER() OVER (ORDER BY Degree DESC) peta_rn, * FROM Achievement WHERE Course=N'C#程序设计') peta_paged
WHERE peta_rn>0 AND peta_rn<=3