PetaPoco 使用總結 一

2021-09-13 09:48:39 字數 2298 閱讀 2481

petapoco 使用總結(一)

petapoco最讓人感興趣的功能包括:

github -

例子:來自petapoco官網

查詢

1.定義poco 類 

//

represents a record in the "articles" table

public

class

article

public

string title

public datetime date_created

public

bool draft

public

string content

}

2.建立petapoco資料庫連線 ,並執行查詢

var db=new petapoco.database("

connectionstringname");

//查詢所有articles

foreach (var a in db.query("

select * from articles"))

- ", a.article_id, a.title);

}

查詢返回引數

long count=db.executescalar("

select count(*) from articles

");

查詢得到一行記錄

var a = db.singleordefault("select * from articles where article_id=0", 123));
分頁查詢petapoco 自帶了分頁功能,可以自動執行分頁請求。

//

var result=db.page(1, 20,  "

select * from articles where category=@0 order by date_posted desc

", "

coolstuff");

/// petapoco 原始碼中,返回的 page 物件 的各個屬性

//////holds the results of a paged request.

/// ///

the type of poco in the returned result set

public

class page

//////

the total number of pages in the full result set

/// public

long totalpages

//////

the total number of records in the full result set

/// public

long totalitems

//////

the number of items per page

/// public

long itemsperpage

//////

the actual records on this page

/// public listitems

//////

user property to hold anything.

/// public

object context

}

query vs fetchdatabase 物件有兩個獲取資料的方法:query 和fetch。這兩個方法非常相似,不同的是fetch方法返回乙個poco類的list<>,而query使用 yield return 迭代所有資料,這些資料並沒有載入到記憶體中。

出現這個問題的時候,一直找不到原因,看了原始碼注釋之後,才知道的。

執行非查詢的sql 語句

使用execute 方法執行乙個不帶查詢的命令:

db.execute("

delete from articles where draft<>0

");

PetaPoco 使用總結 一

petapoco 使用總結 一 petapoco最讓人感興趣的功能包括 github 例子 來自petapoco官網 查詢 1.定義poco 類 represents a record in the articles table public class article public string ...

PetaPoco 使用總結 一

petapoco 使用總結 一 petapoco最讓人感興趣的功能包括 github 例子 來自petapoco官網 查詢 1.定義poco 類 represents a record in the articles table public class article public string ...

git使用總結(一)

最近公司要求使用git作為專案倉庫儲存,所以在研究這個東西,在網上查了很多資料,自己也親自實踐過,然後針對網上的所有內容在結合自己的實踐,做出總結記錄如下 環境 mac pro電腦 git版本 2.6.4 1 建立資料夾 mkdir git 2 進入資料夾 cd git 3 初始化資料夾的git庫 ...