其實(shí),微軟的企業(yè)庫中有一個(gè)非常不錯的數據操作類(lèi)了.但是,不少公司(起碼我遇到的幾個(gè)...),對一些"封裝"了些什么的東西不太敢用,雖然我推薦過(guò)微軟的企業(yè)庫框架了...但是還是要"評估"...一評就是幾個(gè)月...而且,一些公司有的根本就是裸ado.net開(kāi)發(fā),或者自己封裝的數據庫操作類(lèi)非常別扭,很不好用.
這里我給大家共享一個(gè)我參照企業(yè)庫中的數據操作組件編碼風(fēng)格寫(xiě)的數據庫操作類(lèi),對使用它的程序員來(lái)說(shuō),編碼是很舒服滴(起碼我覺(jué)得很好撒).以下是代碼,很簡(jiǎn)單的,沒(méi)有做任何多余的封裝,只是改變了ADO.NET的編碼步驟,方便了具體開(kāi)發(fā)數據庫操作代碼的程序員.
using System;
using System.Data;
using System.Data.Common;
using System.Configuration;
public class DbHelper
public class Trans : IDisposable
那么如何使用它呢?下面我給出一些基本的使用示例,基本能滿(mǎn)足你大部分的數據庫操作需要了.
1)直接執行sql語(yǔ)句
DbHelper db = new DbHelper();
DbCommand cmd = db.GetSqlStringCommond("insert t1 (id)values('haha')");
db.ExecuteNonQuery(cmd); 2)執行存儲過(guò)程
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))
{
dt.Load(reader);
}
string s = db.GetParameter(cmd, "@outString").Value as string;//out parameter
int r = Convert.ToInt32(db.GetParameter(cmd, "@returnValue").Value);//return value
7)事務(wù)的使用.(項目中需要將基本的數據庫操作組合成一個(gè)完整的業(yè)務(wù)流時(shí),代碼級的事務(wù)是必不可少的哦)
pubic void DoBusiness()
{
using (Trans t = new Trans())
{
try
{
D1(t);
throw new Exception();//如果有異常,會(huì )回滾滴
D2(t);
t.Commit();
}
catch
{
t.RollBack();
}
}
}
public void D1(Trans t)
{
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);
if (t == null) db.ExecuteNonQuery(cmd);
else db.ExecuteNonQuery(cmd,t);
string s = db.GetParameter(cmd, "@outString").Value as string;//out parameter
int r = Convert.ToInt32(db.GetParameter(cmd, "@returnValue").Value);//return value
}
public void D2(Trans t)
{
DbHelper db = new DbHelper();
DbCommand cmd = db.GetSqlStringCommond("insert t1 (id)values('..')");
if (t == null) db.ExecuteNonQuery(cmd);
else db.ExecuteNonQuery(cmd, t);
} 以上我們好像沒(méi)有指定數據庫連接字符串,大家如果看下DbHelper的代碼,就知道要使用它必須在config中配置兩個(gè)參數,如下:
<appSettings>
<add key="DbHelperProvider" value="System.Data.SqlClient"/>
<add key="DbHelperConnectionString" value="Data Source=(local);Initial Catalog=DbHelperTest;Persist Security Info=True;User ID=sa;Password=sa"/>
</appSettings> 其實(shí),DbHelper需要的僅僅是兩個(gè)字符串,你可以自己修改,作成加密什么的...
好了,就這樣,DbHelper的代碼是非常簡(jiǎn)單和透明的,只是在ado.net上做了一點(diǎn)小包裝,改變了一下使用它的程序員的編碼方式,去除掉一些比較"物理級"的編程概念,如connection的open和close之類(lèi)的,使程序員更專(zhuān)注于業(yè)務(wù)邏輯代碼的編寫(xiě),少死掉點(diǎn)腦細胞,另外,統一了數據操作層的數據操作代碼的風(fēng)格和格式,維護起來(lái)很方便的撒~~~
另:以上代碼大家可以隨意使用, 不需要給我版權費的啦,嘿嘿.如果大家發(fā)現有什么BUG,或者有更好的數據操作類(lèi)的實(shí)現方式,請聯(lián)系我哦.