C 輕型ORM框架PetaPoco試水

2022-03-04 23:26:36 字數 2509 閱讀 9939

以下是網友根據官方介紹翻譯,這裡貼出來。

petapoco是一款適用於.net 和mono的微小、快速、單檔案的微型orm。

petapoco有以下特色:

如何獲取petapoco?

可以和筆者一樣安裝4.0.3版本

如下圖,回車即可

安裝之後,如果你是.net3.5的編譯環境,請關閉關閉dynamic支援:

1、開啟專案屬性

2、切換到「生成」選項卡

3、在「條件編譯符號」新增petapoco_no_dynamic

我這裡用的是sqlserver資料庫

xml version="1.0" encoding="utf-8" 

?>

<

configuration

>

<

connectionstrings

>

<

add

name

="connectionstring"

connectionstring

="data source=.\sql2008;initial catalog=test;user id=sa;password=123"

providername

="system.data.sqlclient"

/>

connectionstrings

>

configuration

>

然後在t4模板中,填上剛才加的鏈結引數,一儲存就可以自動生成對應的實體

如圖

下面貼出一部分測試**

using

petapocodemo.models;

using

system;

using

system.collections.generic;

using

system.componentmodel;

using

system.data;

using

system.drawing;

using

system.linq;

using

system.text;

using

system.windows.forms;

namespace

petapocodemo

private

void button1_click(object

sender, eventargs e)

//返回乙個scalar數量

var count = db.executescalar("

select count(*) from article");

messagebox.show(

"count:

" +count.tostring());

//返回一行記錄

var row = db.singleordefault("

select * from article where article_id='1'");

messagebox.show(row.content);

//插入記錄

var newarticle = new

article();

newarticle.article_id = 2

; newarticle.title = "綠書"

; newarticle.content = "

可持續發展綠色內容";

newarticle.date_created =datetime.utcnow;

if (convert.toint32(db.insert("

article

", "

article_id

",false, newarticle)) > 0

)

else}}

}

未完待續

C 的ORM 開源框架

odb 是乙個開源的,支援多平台,支援多資料庫的 c 的 orm 框架,可將 c 物件資料庫表對映,進行輕鬆的資料庫查詢和操作。使用odb進行物件持久化具有以下優點 示例 view source print?01odb sqlite database db people.db 02 03person...

ORM框架試驗

public class orm 新增方法 public int add t obj columnnames columns 0 columnname 列名列表 值的列表 values common.getdbvalue columns 0 columntype,convert.tostring p...

C 中流行的ORM框架

net c 主流orm總攬sqlsugar sqlsugar是國人開發者開發的一款基於.net的orm框架,是可以執行在.net 4.net core的高效能 輕量級 orm框架,眾多.net框架中最容易使用的資料庫訪問技術。特點 開源 免費 國內開發者開發 維護 支援.net core 支援主流資...