小男孩‘自慰网亚洲一区二区,亚洲一级在线播放毛片,亚洲中文字幕av每天更新,黄aⅴ永久免费无码,91成人午夜在线精品,色网站免费在线观看,亚洲欧洲wwwww在线观看

分享

DbHelper SQL數(shù)據(jù)操作類【DBHelper.CS】

 悟靜 2012-02-14
其實(shí),微軟的企業(yè)庫(kù)中有一個(gè)非常不錯(cuò)的數(shù)據(jù)操作類了.但是,不少公司(起碼我遇到的幾個(gè)...),對(duì)一些"封裝"了些什么的東西不太敢用,雖然我推薦過(guò)微軟的企業(yè)庫(kù)框架了...但是還是要"評(píng)估"...一評(píng)就是幾個(gè)月...而且,一些公司有的根本就是裸ado.net開(kāi)發(fā),或者自己封裝的數(shù)據(jù)庫(kù)操作類非常別扭,很不好用.
      這里我給大家共享一個(gè)我參照企業(yè)庫(kù)中的數(shù)據(jù)操作組件編碼風(fēng)格寫(xiě)的數(shù)據(jù)庫(kù)操作類,對(duì)使用它的程序員來(lái)說(shuō),編碼是很舒服滴(起碼我覺(jué)得很好撒).以下是代碼,很簡(jiǎn)單的,沒(méi)有做任何多余的封裝,只是改變了ADO.NET的編碼步驟,方便了具體開(kāi)發(fā)數(shù)據(jù)庫(kù)操作代碼的程序員.
    using System;
    
using System.Data;
    
using System.Data.Common;
    
using System.Configuration;

    
public class DbHelper
    


    
public class Trans : IDisposable
    

那么如何使用它呢?下面我給出一些基本的使用示例,基本能滿足你大部分的數(shù)據(jù)庫(kù)操作需要了.
1)直接執(zhí)行sql語(yǔ)句

        DbHelper db = new DbHelper();
        DbCommand cmd 
= db.GetSqlStringCommond("insert t1 (id)values('haha')");
        db.ExecuteNonQuery(cmd);

2)執(zhí)行存儲(chǔ)過(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)輸入?yún)?shù)/輸出參數(shù)/返回值的使用(比較重要哦)

        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ù)的使用.(項(xiàng)目中需要將基本的數(shù)據(jù)庫(kù)操作組合成一個(gè)完整的業(yè)務(wù)流時(shí),代碼級(jí)的事務(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)有指定數(shù)據(jù)庫(kù)連接字符串,大家如果看下DbHelper的代碼,就知道要使用它必須在config中配置兩個(gè)參數(shù),如下:
    <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)小包裝,改變了一下使用它的程序員的編碼方式,去除掉一些比較"物理級(jí)"的編程概念,如connection的open和close之類的,使程序員更專注于業(yè)務(wù)邏輯代碼的編寫(xiě),少死掉點(diǎn)腦細(xì)胞,另外,統(tǒng)一了數(shù)據(jù)操作層的數(shù)據(jù)操作代碼的風(fēng)格和格式,維護(hù)起來(lái)很方便的撒~~~

另:以上代碼大家可以隨意使用, 不需要給我版權(quán)費(fèi)的啦,嘿嘿.如果大家發(fā)現(xiàn)有什么BUG,或者有更好的數(shù)據(jù)操作類的實(shí)現(xiàn)方式,請(qǐng)聯(lián)系我哦.

    本站是提供個(gè)人知識(shí)管理的網(wǎng)絡(luò)存儲(chǔ)空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點(diǎn)。請(qǐng)注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購(gòu)買(mǎi)等信息,謹(jǐn)防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請(qǐng)點(diǎn)擊一鍵舉報(bào)。
    轉(zhuǎn)藏 分享 獻(xiàn)花(0

    0條評(píng)論

    發(fā)表

    請(qǐng)遵守用戶 評(píng)論公約

    類似文章 更多