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