DBHelper 类(网上收集)

这个是我网上找的,觉得不错的一个DBHelper类,下面是作者话:

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

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data;
using System.Data.Common;
using System.Configuration;

namespace EscapeeCompareService
{
    public class DBHelper
    {
        private static string dbProviderName = ConfigurationManager.AppSettings["DbHelperProvider"];
        private static string dbConnectionString = ConfigurationManager.AppSettings["DbHelperConnectionString"];

        private DbConnection connection;
        public DBHelper()
        {
            this.connection = CreateConnection(DBHelper.dbConnectionString);
        }

        public DBHelper(string connectionString)
        {
            this.connection = CreateConnection(connectionString);
        }

        public static DbConnection CreateConnection()
        {
            DbProviderFactory dbfactory = DbProviderFactories.GetFactory(DBHelper.dbProviderName);
            DbConnection dbconn = dbfactory.CreateConnection();
            dbconn.ConnectionString = DBHelper.dbConnectionString;
            return dbconn;
        }

        public static DbConnection CreateConnection(string connectionString)
        {
            DbProviderFactory dbfactory = DbProviderFactories.GetFactory(DBHelper.dbProviderName);
            DbConnection dbconn = dbfactory.CreateConnection();
            dbconn.ConnectionString = connectionString;
            return dbconn;
        }

        public DbCommand GetStoredProcCommond(string storedProcedure)
        {
            DbCommand dbCommand = connection.CreateCommand();
            dbCommand.CommandText = storedProcedure;
            dbCommand.CommandType = CommandType.StoredProcedure;
            return dbCommand;
        }
        public DbCommand GetSqlStringCommond(string sqlQuery)
        {
            DbCommand dbCommand = connection.CreateCommand();
            dbCommand.CommandText = sqlQuery;
            dbCommand.CommandType = CommandType.Text;
            return dbCommand;
        }

        //增加参数#region 增加参数
        #region 增加参数#region 增加参数
        public void AddParameterCollection(DbCommand cmd, DbParameterCollection dbParameterCollection)
        {
            foreach (DbParameter dbParameter in dbParameterCollection)
            {
                cmd.Parameters.Add(dbParameter);
            }
        }

        public void AddOutParameter(DbCommand cmd, string parameterName, DbType dbType, int size)
        {
            DbParameter dbParameter = cmd.CreateParameter();
            dbParameter.DbType = dbType;
            dbParameter.ParameterName = parameterName;
            dbParameter.Size = size;
            dbParameter.Direction = ParameterDirection.Output;
            cmd.Parameters.Add(dbParameter);
        }

        public void AddInParameter(DbCommand cmd, string parameterName, DbType dbType, object value)
        {
            DbParameter dbParameter = cmd.CreateParameter();
            dbParameter.DbType = dbType;
            dbParameter.ParameterName = parameterName;
            dbParameter.Value = value;
            dbParameter.Direction = ParameterDirection.Input;
            cmd.Parameters.Add(dbParameter);
        }

        public void AddReturnParameter(DbCommand cmd, string parameterName, DbType dbType)
        {
            DbParameter dbParameter = cmd.CreateParameter();
            dbParameter.DbType = dbType;
            dbParameter.ParameterName = parameterName;
            dbParameter.Direction = ParameterDirection.ReturnValue;
            cmd.Parameters.Add(dbParameter);
        }

        public DbParameter GetParameter(DbCommand cmd, string parameterName)
        {
            return cmd.Parameters[parameterName];
        }

        #endregion

        //执行#region 执行
        #region 执行#region 执行

        public DataSet ExecuteDataSet(DbCommand cmd)
        {
            DbProviderFactory dbfactory = DbProviderFactories.GetFactory(DBHelper.dbProviderName);
            DbDataAdapter dbDataAdapter = dbfactory.CreateDataAdapter();
            dbDataAdapter.SelectCommand = cmd;
            DataSet ds = new DataSet();
            dbDataAdapter.Fill(ds);
            return ds;
        }

        public DataTable ExecuteDataTable(DbCommand cmd)
        {
            DbProviderFactory dbfactory = DbProviderFactories.GetFactory(DBHelper.dbProviderName);
            DbDataAdapter dbDataAdapter = dbfactory.CreateDataAdapter();
            dbDataAdapter.SelectCommand = cmd;
            DataTable dataTable = new DataTable();
            dbDataAdapter.Fill(dataTable);
            return dataTable;
        }

        public DbDataReader ExecuteReader(DbCommand cmd)
        {
            cmd.Connection.Open();
            DbDataReader reader = cmd.ExecuteReader(CommandBehavior.CloseConnection);
            return reader;
        }

        public int ExecuteNonQuery(DbCommand cmd)
        {
            cmd.Connection.Open();
            int ret = cmd.ExecuteNonQuery();
            cmd.Connection.Close();
            return ret;
        }

        public object ExecuteScalar(DbCommand cmd)
        {
            cmd.Connection.Open();
            object ret = cmd.ExecuteScalar();
            cmd.Connection.Close();
            return ret;
        }

        #endregion

        //执行事务#region 执行事务
        #region 执行事务#region 执行事务
        public DataSet ExecuteDataSet(DbCommand cmd, Trans t)
        {
            cmd.Connection = t.DbConnection;
            cmd.Transaction = t.DbTrans;
            DbProviderFactory dbfactory = DbProviderFactories.GetFactory(DBHelper.dbProviderName);
            DbDataAdapter dbDataAdapter = dbfactory.CreateDataAdapter();
            dbDataAdapter.SelectCommand = cmd;
            DataSet ds = new DataSet();
            dbDataAdapter.Fill(ds);
            return ds;
        }

        public DataTable ExecuteDataTable(DbCommand cmd, Trans t)
        {
            cmd.Connection = t.DbConnection;
            cmd.Transaction = t.DbTrans;
            DbProviderFactory dbfactory = DbProviderFactories.GetFactory(DBHelper.dbProviderName);
            DbDataAdapter dbDataAdapter = dbfactory.CreateDataAdapter();
            dbDataAdapter.SelectCommand = cmd;
            DataTable dataTable = new DataTable();
            dbDataAdapter.Fill(dataTable);
            return dataTable;
        }

        public DbDataReader ExecuteReader(DbCommand cmd, Trans t)
        {
            cmd.Connection.Close();
            cmd.Connection = t.DbConnection;
            cmd.Transaction = t.DbTrans;
            DbDataReader reader = cmd.ExecuteReader();
            DataTable dt = new DataTable();
            return reader;
        }

        public int ExecuteNonQuery(DbCommand cmd, Trans t)
        {
            cmd.Connection.Close();
            cmd.Connection = t.DbConnection;
            cmd.Transaction = t.DbTrans;
            int ret = cmd.ExecuteNonQuery();
            return ret;
        }

        public object ExecuteScalar(DbCommand cmd, Trans t)
        {
            cmd.Connection.Close();
            cmd.Connection = t.DbConnection;
            cmd.Transaction = t.DbTrans;
            object ret = cmd.ExecuteScalar();
            return ret;
        }

        #endregion
    }

    public class Trans : IDisposable
    {
        private DbConnection conn;
        private DbTransaction dbTrans;
        public DbConnection DbConnection
        {
            get { return this.conn; }
        }
        public DbTransaction DbTrans
        {
            get { return this.dbTrans; }
        }

        public Trans()
        {
            conn = DBHelper.CreateConnection();
            conn.Open();
            dbTrans = conn.BeginTransaction();
        }
        public Trans(string connectionString)
        {
            conn = DBHelper.CreateConnection(connectionString);
            conn.Open();
            dbTrans = conn.BeginTransaction();
        }
        public void Commit()
        {
            dbTrans.Commit();
            this.Colse();
        }

        public void RollBack()
        {
            dbTrans.Rollback();
            this.Colse();
        }

        public void Dispose()
        {
            this.Colse();
        }

        public void Colse()
        {
            if (conn.State == System.Data.ConnectionState.Open)
            {
                conn.Close();
            }
        }
    }
}

那么如何使用它呢?下面我给出一些基本的使用示例,基本能满足你大部分的数据库操作需要了.
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))
{
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)事务的使用.(项目中需要将基本的数据库操作组合成一个完整的业务流时,代码级的事务是必不可少的哦)

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之类的,使程序员更专注于业务逻辑代码的编写,少死掉点脑细胞,另外,统一了数据 操作层的数据操作代码的风格和格式,维护起来很方便的

时间: 2024-10-07 20:46:39

DBHelper 类(网上收集)的相关文章

js日期处理类(网上收集)

/*** 日期处理工具类*/ var DateUtil = function(){ /**     * 判断闰年     * @param date Date日期对象     * @return boolean true 或false     */    this.isLeapYear = function(date){        return (0==date.getYear()%4&&((date.getYear()%100!=0)||(date.getYear()%400==0)

Android 系统工具类SystemUtils收集整理(持续更新)

最近做的功能中涉及到了一些关于系统方面的东西,自己摸索以及网上搜集整理出来了一个工具类方便调用 包含的功能有: 获取系统中所有APP应用.获取用户安装的APP应用.根据包名和Activity启动类查询应用信息.跳转到WIFI设置.WIFI网络开关.移动网络开关.GPS开关 当前若关则打开 当前若开则关闭.调节系统音量.设置亮度.获取屏幕的亮度.跳转到系统设置.获取文件夹下所有文件.获取视频的缩略图 .打开视频文件... 工具类会持续更新,与大家共同学习进步. SystemUtils.java p

Android 阅读器架构图,网上收集,留做存货

这个结构图是网上收集的图片.基结构明晰简洁.易于后期维护.本文会继续收集很多其他其他优秀的结构图,望有图的朋友推荐~

Java数据库访问:DBHelper类

压缩包中包含3部分: 1.DBHelper类 2.ResultSet使用详解 3.DBHelper类用法举例 代码下载

常用正则表达式(网上收集的)

常用的正则表达式 常用正则表达式 正则表达式用于字符串处理.表单验证等场合,实用高效.现将一些常用的表达式收集于此,以备不时之需. 用户名:/^[a-z0-9_-]{3,16}$/ 密码:/^[a-z0-9_-]{6,18}$/ 十六进制值:/^#?([a-f0-9]{6}|[a-f0-9]{3})$/ 电子邮箱:/^([a-z0-9_\.-]+)@([\da-z\.-]+)\.([a-z\.]{2,6})$/ URL:/^(https?:\/\/)?([\da-z\.-]+)\.([a-z\.

C# 网上收集的一些所谓的开源项目

C#开源 商业协作和项目管理平台-TeamLab 网络视频会议软件-VMukti 驰骋工作流程引擎-ccflow [免费]正则表达式测试工具-Regex-Tester Windows-Phone-7-SDK Excel-读写组件-ExcelLibrary .NET集成开发环境-MonoDevelop 电话软交换机-FreeSWITCH 开源操作系统-Cosmos 坦克机器人战斗仿真引擎-Robocode GIS控件-MapWindow .NET的ORM框架-MyBatis.NET Web开发工具

Ado.net中简单的DBHelper类(增删改查)

private static string connString = "server=.;database=hotel;uid=aa;pwd=123";//最好从配置文件中取出 public static SqlConnection conn = new SqlConnection(connString); //公用的增删改方法 public static bool ZhengShanGai(string sql) { bool flag = false; SqlCommand com

C# DBHelper类

class DBHelper { //public static string connstr = "server=.;database=SuperKTV;uid=sa;pwd=123456"; public static string connstr = "server=.;database=Dormitory;uid=sa;pwd=123456"; //数据库链接对象 private static SqlConnection Conn = null; //初始化

能用存储过程的DBHelper类

using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Data; using System.Data.SqlClient; namespace Book.DAL { public class DBHelper { private SqlConnection conn = null; private SqlCommand cmd = null; priva