DbHelper資料操作類

2021-06-06 09:43:10 字數 4058 閱讀 9678

using system;

using system.data;

using system.data.common;

using system.configuration;

public class dbhelper

public dbhelper(string connectionstring)

public static dbconnection createconnection()

public static dbconnection createconnection(string connectionstring)

public dbcommand getstoredproccommond(string storedprocedure)

public dbcommand getsqlstringcommond(string sqlquery)

#region 增加引數

public void addparametercollection(dbcommand cmd, dbparametercollection dbparametercollection)

}public void addoutparameter(dbcommand cmd, string parametername, dbtype dbtype, int size)

public void addinparameter(dbcommand cmd, string parametername, dbtype dbtype, object value)

public void addreturnparameter(dbcommand cmd, string parametername, dbtype dbtype)

public dbparameter getparameter(dbcommand cmd, string parametername)

#endregion

#region 執行

public dataset executedataset(dbcommand cmd)

public datatable executedatatable(dbcommand cmd)

public dbdatareader executereader(dbcommand cmd)

public int executenonquery(dbcommand cmd)

public object executescalar(dbcommand cmd)

#endregion

#region 執行事務

public dataset executedataset(dbcommand cmd,trans t)

public datatable executedatatable(dbcommand cmd, trans t)

public dbdatareader executereader(dbcommand cmd, trans t)

public int executenonquery(dbcommand cmd, trans t)

public object executescalar(dbcommand cmd, trans t)

#endregion

}public class trans : idisposable

}public dbtransaction dbtrans

}public trans()

public trans(string connectionstring)

public void commit()

public void rollback()

public void dispose()

public void colse()

}}

那麼如何使用它呢?下面我給出一些基本的使用示例,基本能滿足你大部分的資料庫操作需要了.

1)直接執行sql語句

dbhelper db = new dbhelper();

dbcommand cmd = db.getsqlstringcommond("insert t1 (id)values('haha')");

db.executenonquery(cmd);

2)執行儲存過程

dbhelper db = new dbhelper();

dbcommand cmd = db.getstoredproccommond("t1_insert");

db.addinparameter(cmd, "@id", dbtype.string, "heihei");

db.executenonquery(cmd);

3)返回dataset
dbhelper db = new dbhelper();

dbcommand cmd = db.getsqlstringcommond("select * from t1");

dataset ds = db.executedataset(cmd);

4)返回datatable

dbhelper db = new dbhelper();

dbcommand cmd = db.getsqlstringcommond("t1_findall");

datatable dt = db.executedatatable(cmd);

5)輸入引數/輸出引數/返回值的使用

dbhelper db = new dbhelper();

dbcommand cmd = 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 as string;//out parameter

int r = convert.toint32(db.getparameter(cmd, "@returnvalue").value);//return value

6)datareader使用

dbhelper db = new dbhelper();

dbcommand cmd = 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 (dbdatareader reader = db.executereader(cmd))

string s = db.getparameter(cmd, "@outstring").value as string;//out parameter

int r = convert.toint32(db.getparameter(cmd, "@returnvalue").value);//return value

7)事務的使用.(專案中需要將基本的資料庫操作組合成乙個完整的業務流時,**級的事務是必不可少的哦)

pubic void dobusiness()

catch}}

public void d1(trans t)

public void d2(trans t)

我的DbHelper資料操作類

其實,微軟的企業庫中有乙個非常不錯的資料操作類了.但是,不少公司 起碼我遇到的幾個.對一些 封裝 了些什麼的東西不太敢用,雖然我推薦過微軟的企業庫框架了.但是還是要 評估 一評就是幾個月.而且,一些公司有的根本就是裸ado.net開發,或者自己封裝的資料庫操作類非常彆扭,很不好用.這裡我給大家共享乙...

DBHelper 資料庫操作封裝類

using system using system.collections.generic using system.text using system.data using system.data.sqlclient using system.configuration namespace boo...

自己整理的的資料操作DbHelper

using system.data using system.data.sqlclient using system.configuration namespace return ds.tables 0 查詢資料庫是否存在資料 查詢sql語句 查詢引數 存在返回true,不存在返回false pub...