其实,微软的企业库中有一个非常不错的数据操作类了.但是,不少公司(起码我遇到的几个...),对一些"封装"了些什么的东西不太敢用,虽然我推荐过微软的企业库框架了...但是还是要"评估"...一评就是几个月...而且,一些公司有的根本就是裸ado.net开发,或者自己封装的数据库操作类非常别扭,很不好用.
      这里我给大家共享一个我参照企业库中的数据操作组件编码风格写的数据库操作类,对使用它的程序员来说,编码是很舒服滴(起码我觉得很好撒).以下是代码,很简单的,没有做任何多余的封装,只是改变了ADO.NET的编码步骤,方便了具体开发数据库操作代码的程序员.
我的DbHelper数据操作类    using System;
我的DbHelper数据操作类    
using System.Data;
我的DbHelper数据操作类    
using System.Data.Common;
我的DbHelper数据操作类    
using System.Configuration;
我的DbHelper数据操作类
我的DbHelper数据操作类    
public class DbHelper
    }

那么如何使用它呢?下面我给出一些基本的使用示例,基本能满足你大部分的数据库操作需要了.
1)直接执行sql语句

我的DbHelper数据操作类        DbHelper db = new DbHelper();
我的DbHelper数据操作类        DbCommand cmd 
= db.GetSqlStringCommond("insert t1 (id)values('haha')");
我的DbHelper数据操作类        db.ExecuteNonQuery(cmd);

2)执行存储过程

我的DbHelper数据操作类        DbHelper db = new DbHelper();
我的DbHelper数据操作类        DbCommand cmd 
= db.GetStoredProcCommond("t1_insert");
我的DbHelper数据操作类        db.AddInParameter(cmd, 
"@id", DbType.String, "heihei");
我的DbHelper数据操作类        db.ExecuteNonQuery(cmd);

3)返回DataSet

我的DbHelper数据操作类        DbHelper db = new DbHelper();
我的DbHelper数据操作类        DbCommand cmd 
= db.GetSqlStringCommond("select * from t1");
我的DbHelper数据操作类        DataSet ds 
= db.ExecuteDataSet(cmd);

4)返回DataTable

我的DbHelper数据操作类        DbHelper db = new DbHelper();
我的DbHelper数据操作类        DbCommand cmd 
= db.GetSqlStringCommond("t1_findall");
我的DbHelper数据操作类        DataTable dt 
= db.ExecuteDataTable(cmd);

5)输入参数/输出参数/返回值的使用(比较重要哦)

我的DbHelper数据操作类        DbHelper db = new DbHelper();
我的DbHelper数据操作类        DbCommand cmd 
= db.GetStoredProcCommond("t2_insert");
我的DbHelper数据操作类        db.AddInParameter(cmd, 
"@timeticks", DbType.Int64, DateTime.Now.Ticks);
我的DbHelper数据操作类        db.AddOutParameter(cmd, 
"@outString", DbType.String, 20);
我的DbHelper数据操作类        db.AddReturnParameter(cmd, 
"@returnValue", DbType.Int32);
我的DbHelper数据操作类
我的DbHelper数据操作类        db.ExecuteNonQuery(cmd);
我的DbHelper数据操作类
我的DbHelper数据操作类        
string s = db.GetParameter(cmd, "@outString").Value as string;//out parameter
我的DbHelper数据操作类
        int r = Convert.ToInt32(db.GetParameter(cmd, "@returnValue").Value);//return value
我的DbHelper数据操作类

6)DataReader使用

我的DbHelper数据操作类      DbHelper db = new DbHelper();
我的DbHelper数据操作类        DbCommand cmd 
= db.GetStoredProcCommond("t2_insert");
我的DbHelper数据操作类        db.AddInParameter(cmd, 
"@timeticks", DbType.Int64, DateTime.Now.Ticks);
我的DbHelper数据操作类        db.AddOutParameter(cmd, 
"@outString", DbType.String, 20);
我的DbHelper数据操作类        db.AddReturnParameter(cmd, 
"@returnValue", DbType.Int32);
我的DbHelper数据操作类
我的DbHelper数据操作类        
using (DbDataReader reader = db.ExecuteReader(cmd))

7)事务的使用.(项目中需要将基本的数据库操作组合成一个完整的业务流时,代码级的事务是必不可少的哦)
我的DbHelper数据操作类    pubic void DoBusiness()
    }

以上我们好像没有指定数据库连接字符串,大家如果看下DbHelper的代码,就知道要使用它必须在config中配置两个参数,如下:
我的DbHelper数据操作类    <appSettings>
我的DbHelper数据操作类        
<add key="DbHelperProvider" value="System.Data.SqlClient"/>
我的DbHelper数据操作类        
<add key="DbHelperConnectionString" value="Data Source=(local);Initial Catalog=DbHelperTest;Persist Security Info=True;User ID=sa;Password=sa"/>
我的DbHelper数据操作类    
</appSettings>
其实,DbHelper需要的仅仅是两个字符串,你可以自己修改,作成加密什么的...

好了,就这样,DbHelper的代码是非常简单和透明的,只是在ado.net上做了一点小包装,改变了一下使用它的程序员的编码方式,去除掉一些比较"物理级"的编程概念,如connection的open和close之类的,使程序员更专注于业务逻辑代码的编写,少死掉点脑细胞,另外,统一了数据操作层的数据操作代码的风格和格式,维护起来很方便的撒~~~

另:以上代码大家可以随意使用, 不需要给我版权费的啦,嘿嘿.如果大家发现有什么BUG,或者有更好的数据操作类的实现方式,请联系我哦.

相关文章:

  • 2021-11-20
  • 2022-12-23
  • 2021-10-31
  • 2022-12-23
  • 2022-12-23
  • 2021-09-10
猜你喜欢
  • 2021-06-17
  • 2022-01-11
相关资源
相似解决方案