view plain
using
system;
using
system.configuration;
using
system.data;
using
system.data.common;
using
system.data.oledb;
using
system.data.sqlclient;
namespace
lelog.data
public
dbhelper(
string
connectionstring)
public
static
dbconnectioncreateconnection()
public
static
dbconnectioncreateconnection(
string
connectionstring)
public
dbcommandgetstoredproccommond(
string
storedprocedure)
public
dbcommandgetsqlstringcommond(
string
sqlquery)
#region增加引數
public
void
addparametercollection(dbcommandcmd,dbparametercollectiondbparametercollection) }
public
void
addoutparameter(dbcommandcmd,
string
parametername,dbtypedbtype,
intsize)
public
void
addinparameter(dbcommandcmd,
string
parametername,dbtypedbtype,
object
value)
public
void
addreturnparameter(dbcommandcmd,
string
parametername,dbtypedbtype)
public
dbparametergetparameter(dbcommandcmd,
string
parametername)
#endregion
#region執行
public
datasetexecutedataset(dbcommandcmd)
public
datatableexecutedatatable(dbcommandcmd)
public
dbdatareaderexecutereader(dbcommandcmd)
public
intexecutenonquery(dbcommandcmd)
public
object
executescalar(dbcommandcmd)
#endregion
#region執行事務
public
datasetexecutedataset(dbcommandcmd,transt)
public
datatableexecutedatatable(dbcommandcmd,transt)
public
dbdatareaderexecutereader(dbcommandcmd,transt)
public
intexecutenonquery(dbcommandcmd,transt)
public
object
executescalar(dbcommandcmd,transt)
#endregion }
public
class
trans:idisposable
}public
dbtransactiondbtrans
}public
trans()
public
trans(
string
connectionstring)
public
void
commit()
public
void
rollback()
public
void
dispose()
public
void
colse() }
}}2、直接執行sql語句
view plain
dbhelperdb=
newdbhelper();
dbcommandcmd=db.getsqlstringcommond("insertt1(id)values(『?haha『?)");
db.executenonquery(cmd);
3、執行儲存過程
view plain
dbhelperdb=
newdbhelper();
dbcommandcmd=db.getstoredproccommond("t1_insert");
db.addinparameter(cmd,"@id"
,dbtype.string,
"heihei");
db.executenonquery(cmd);
4、返回dataset
view plain
dbhelperdb=
newdbhelper();
dbcommandcmd=db.getsqlstringcommond("select*fromt1");
datasetds=db.executedataset(cmd);
5、返回datatable
view plain
dbhelperdb=
newdbhelper();
dbcommandcmd=db.getsqlstringcommond("t1_findall");
datatabledt=db.executedatatable(cmd);
6、輸入引數/輸出引數/返回值的使用(比較重要哦)
view plain
dbhelperdb=
newdbhelper();
dbcommandcmd=db.getstoredproccommond("t2_insert");
db.addinparameter(cmd,"@timeticks"
,dbtype.int64,datetime.now.ticks);
db.addoutparameter(cmd,"@outstring"
,dbtype.string,20);
db.addreturnparameter(cmd,"@returnvalue"
,dbtype.int32);
db.executenonquery(cmd);
string
s=db.getparameter(cmd,
"@outstring"
).value
asstring;
intr=convert.toint32(db.getparameter(cmd,
"@returnvalue"
).value);
7、datareader使用
view plain
dbhelperdb=
newdbhelper();
dbcommandcmd=db.getstoredproccommond("t2_insert");
db.addinparameter(cmd,"@timeticks"
,dbtype.int64,datetime.now.ticks);
db.addoutparameter(cmd,"@outstring"
,dbtype.string,20);
db.addreturnparameter(cmd,"@returnvalue"
,dbtype.int32);
using
(dbdatareaderreader=db.executereader(cmd))
string
s=db.getparameter(cmd,
"@outstring"
).value
asstring;
intr=convert.toint32(db.getparameter(cmd,
"@returnvalue"
).value);
8、事務的使用.(專案中需要將基本的資料庫操作組合成乙個完整的業務流時,**級的事務是必不可少的哦)
以上我們好像沒有指定資料庫連線字串,大家如果看下dbhelper的**,就知道要使用它必須在config中配置兩個引數,如下:
view plain
pubic
void
dobusiness()
catch }
}public
void
d1(transt)
public
void
d2(transt)
DbHelper資料庫通用類使用方法
執行sql語句 public static void execsqlcommand 執行儲存過程 public static void execstoredprocedure 返回datatable public static void getdatatable 獲取各種返回值 public sta...
能用儲存過程的DBHelper類
建立人 陳偉 說明 資料庫助手類 using system using system.collections.generic using system.linq using system.text using system.data using system.data.sqlclient names...
我的DbHelper資料操作類
其實,微軟的企業庫中有乙個非常不錯的資料操作類了.但是,不少公司 起碼我遇到的幾個.對一些 封裝 了些什麼的東西不太敢用,雖然我推薦過微軟的企業庫框架了.但是還是要 評估 一評就是幾個月.而且,一些公司有的根本就是裸ado.net開發,或者自己封裝的資料庫操作類非常彆扭,很不好用.這裡我給大家共享乙...