乙個簡單的ORM製作 SQL幫助類

2021-06-28 03:24:56 字數 3096 閱讀 1026

乙個簡單的orm製作大概需要以下幾個類:

sql執行類

curd操作類

其他醬油類

先從sql執行類

說起,可能會涉及資料庫的遷移

等問題,所以需要定義乙個介面以方便遷移到其他資料庫,

事務沒提供命名,若需要命名可修改為可變引數,ihelper**如下:

internal

inte***ce

ihelper:idisposable

//獲取insert產生id的sql**,可能各個資料庫不太相同

string parstr(string name);//

引數標示符,比如mssql:@name, mysql:?name,odbc直接返回?無命名

}

以上為我個人能思考到的資料庫差異和基礎功能,若有其他請大家指教

通用mssql幫助類**如下:

class

mssql:ihelper

void setcmd(string str, ienumerableps, bool

issp)

}void closecon()}

void opencon() }

int ihelper.executequery(string str, ienumerableps, bool

issp)

finally}

}object ihelper.exectuescalar(string str, ienumerableps, bool

issp)

finally}

}idatareader ihelper.exectuereader(

string str, ienumerableps, bool

issp)

void ihelper.begtran()

void ihelper.commit() }

void ihelper.rollback() }

string ihelper.createsql(string

select, string tbname, string

where, string

orderby, int size, int

index)

select+"

from ");

(select row_number() over(

" + (!string.isnullorempty(orderby)?"

order by

"+orderby:string.empty) + "

) as rowid,

" + select + "

from

");}

where

" + where : string

.empty);

if (index > 1

)

else

return

sb.tostring();

}idataparameter ihelper.cp(

string name, object value)

string ihelper.parstr(string name)

string ihelper.getidstr }

void

idisposable.dispose()

}

mssql2005開始支援mars,預設為關閉狀態,也可以重寫個支援mars的help,預設開啟2個連線,乙個用於查詢乙個用於無事物的執行,**如下:

class msmars:ihelper//

自動開啟mars,連線0為查詢連線,連線1為執行無事務連線,事務連線為新連線

}static sqlcommand getcmd(int i = 3

)

if (i >=

3
) 

if (globalcon != null)

lock

(ck)

globalcon = new sqlconnection[2] ;

globalcon[

0].open(); globalcon[1

].open();

}return

globalcon[i].createcommand();

}sqlcommand cmd;

void setcmd(string txt, ienumerableps, bool

issp)

public

int executequery(string txt, ienumerableps, bool

issp)

public

object exectuescalar(string txt, ienumerableps, bool

issp)

public idatareader exectuereader(string txt, ienumerableps, bool

issp)

public

void

begtran()

public

void

rollback()

public

void

commit()

public

string createsql(string

select, string tbname, string

where, string

orderby, int size, int

index)...

public idataparameter cp(string name, object

value)...

public

string getidstr...

public

string parstr(string

name)...

public

void dispose()...

乙個簡單的SQL查詢,尋求幫助

這是我的期末作業 我做了乙個有資料庫的查詢系統。查詢表單上有4個查詢入口,分別為姓名 txtname 性別 txt 年齡 txtage 成績 txtscore 使用者可以選擇輸入1個或幾個進行查詢。但我不知道sql語句該怎麼寫。如果寫成sql select from student where 姓名...

乙個簡單的sql

declare wokno varchar 500 用來記錄職工號 declare str nvarchar 4000 用來存放查詢語句 declare count int 求出總記錄數 declare i int set i 0 select count count distinct wokno ...

乙個簡單的乙個sql表遍歷

簡單的乙個sql表遍歷 一般我們寫儲存過程或者其他sql語句的時候都會用到迴圈遍歷資料,最常用的兩種就是 1 游標 2 臨時表 while 下面貼出示例 declare minrelogid int 這裡的 minrelogid 一般都是表中的主鍵 select top 1 minrelogid m...