码迷,mamicode.com
首页 > 数据库 > 详细

简单封装DBHelper

时间:2015-09-28 18:45:30      阅读:284      评论:0      收藏:0      [点我收藏+]

标签:

面向对象三大特性:封装,继承,多态。

从刚开始学习编码直到现在,虽然这些特性天天在用,但是感觉自己理解的还是很浅,DBHelper一般大家都应该是写一个dll去调用

最终的运行结果为:技术分享

遇见的问题:

(1).代码冗余(或多或少还是有冗余的部分代码).

(2).连接字符串问题(代码中注释部分).

表结构:

create table t
(
    v varchar(max) null
)

DBHelper:

using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data.Common;
using System.Data.SqlClient;
using System.Data;
namespace DataBase
{
    /// <summary>
    /// DBHelper
    /// </summary>
    public class Mssql
    {
        /// <summary>
        /// 在这里,怎么才可以把这个dll直接发给别人让别人直接去调用而不再需要知道连接字符串的名字
        /// </summary>
        private static readonly String conStr = ConfigurationManager.ConnectionStrings["mssql"].ConnectionString;
        private DbConnection GetCon()
        {
            return new SqlConnection(conStr);
        }
        private SqlConnection Connection()
        {
            return new SqlConnection(conStr);
        }
        /// <summary>
        /// 执行SQL(Insert,Delete,Update)
        /// </summary>
        /// <param name="sql"></param>
        /// <param name="pms"></param>
        /// <returns></returns>
        public Int32 ExecNonQuery(String sql, params DbParameter[] pms)
        {
            Int32 r;
            SqlConnection con = Connection();
            using (DbCommand cmd = new SqlCommand(sql, con))
            {
                if (null != pms)
                {
                    cmd.Parameters.AddRange(pms);
                }
                con.Open();
                r = cmd.ExecuteNonQuery();
                con.Close();
                con.Dispose();
            }
            return r;
        }
        /// <summary>
        /// 查询语句(独占一个连接)
        /// </summary>
        /// <param name="sql"></param>
        /// <param name="pms"></param>
        /// <returns></returns>
        public SqlDataReader ExecReader(String sql, params DbParameter[] pms)
        {
            SqlConnection con = Connection();
            using (DbCommand cmd = new SqlCommand(sql, con))
            {
                if (null != pms)
                {
                    cmd.Parameters.AddRange(pms);
                }
                con.Open();
                return cmd.ExecuteReader(CommandBehavior.CloseConnection) as SqlDataReader;
            }
        }
        /// <summary>
        /// 断开式
        /// </summary>
        /// <param name="sql"></param>
        /// <param name="pms"></param>
        /// <returns></returns>
        public DataTable ExecTable(String sql, params DbParameter[] pms)
        {
            DataTable dt;
            SqlConnection con = Connection();
            using (SqlDataAdapter sda = new SqlDataAdapter(sql, con))
            {
                if (null != pms)
                {
                    sda.SelectCommand.Parameters.AddRange(pms);
                }
                dt = new DataTable();
                sda.Fill(dt);
                con.Close();
                con.Dispose();
            }
            return dt;
        }
        /// <summary>
        /// 返回首行首列
        /// </summary>
        /// <param name="sql"></param>
        /// <param name="pms"></param>
        /// <returns></returns>
        public Object ExecScalar(String sql, params DbParameter[] pms)
        {
            Object obj;
            SqlConnection con = Connection();
            using (DbCommand cmd = new SqlCommand(sql, con))
            {
                if (null != pms)
                {
                    cmd.Parameters.AddRange(pms);
                }
                con.Open();
                obj = cmd.ExecuteScalar();
                con.Close();
                con.Dispose();
            }
            return obj;
        }
        public DataSet ExecDataSet(String sql, params DbParameter[] pms)
        {
            DataSet ds;
            SqlConnection con = Connection();
            using (SqlDataAdapter sda = new SqlDataAdapter(sql, con))
            {
                if (null != pms)
                {
                    sda.SelectCommand.Parameters.AddRange(pms);
                }
                con.Open();
                ds = new DataSet();
                sda.Fill(ds);
                con.Close();
                con.Dispose();
            }
            return ds;
        }
        private DataTable ExecTable(string procName, CommandType type, params DbParameter[] pms)
        {
            DataTable dt;
            SqlConnection con = Connection();
            using (SqlDataAdapter sda = new SqlDataAdapter(procName, con))
            {
                if (null != pms)
                {
                    sda.SelectCommand.Parameters.AddRange(pms);
                }
                sda.SelectCommand.CommandType = type;
                dt = new DataTable();
                sda.Fill(dt);
                con.Open();
                con.Close();
                con.Dispose();
            }
            return dt;
        }
        /// <summary>
        /// 执行存储过程
        /// </summary>
        /// <param name="procName"></param>
        /// <param name="pms"></param>
        /// <returns></returns>
        public DataTable ExecProc(String procName, params DbParameter[] pms)
        {
            return ExecTable(procName, CommandType.StoredProcedure, pms);
        }
    }
}

主程序:

using DataBase;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
namespace TestDll
{
    /// <summary>
    /// Main
    /// </summary>
    class Program
    {
        static void Main(string[] args)
        {
            String result = String.Empty;
            Mssql db = new Mssql();
            #region 新增
            String sql = "insert into t values(@args)";
            Int32 r = db.ExecNonQuery(sql, new SqlParameter("@args", "大声说出:我爱贺晓冬"));
            if (r > 0)
            {
                result = "数据插入成功,[恭喜你成功爱上贺晓冬],";
            }
            sql = "select * from t";
            #endregion
            #region 查询
            SqlDataReader reader = db.ExecReader(sql);
            if (reader.HasRows)
            {
                while (reader.Read())
                {
                    result += "读取到的数据为:" + reader.GetString(0);
                }
            }
            Console.WriteLine(result);
            #endregion
            #region 存储过程
            String procName = "usp_t";
            DataTable dt = db.ExecProc(procName);
            #endregion
            Console.ReadKey();
        }
    }
}

 

简单封装DBHelper

标签:

原文地址:http://www.cnblogs.com/hexd1230/p/4844565.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!