DBHelper 类

2016-11-03
2分钟阅读时长
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data;
using System.Data.SqlClient;

namespace GFAMS
{
    class DBHelper
    {
        //数据库连接字符串
        private static string connStr = "Data Source=.;Initial Catalog=数据库名;Integrated Security=True";

        /// <summary>
        /// 内部数据连接对象
        /// </summary>
        private static SqlConnection Conn = null;

        /// <summary>
        /// 内部实例对象
        /// </summary>
        private static DBHelper Instance = null;

        private DBHelper()
        {

        }

        /// <summary>
        /// 静态方法,获取数据库连接实例
        /// </summary>
        /// <returns>数据库连接实例</returns>
        public static DBHelper GetInstance()
        {
            if (DBHelper.Instance == null)
            {
                DBHelper.Instance = new DBHelper();
            }
            return DBHelper.Instance;
        }

        /// <summary>
        /// 初始化数据库连接
        /// </summary>
        public void InitConnection()
        {
            //如果连接对象不存在,则创建连接
            if (Conn == null)
                Conn = new SqlConnection(connStr);
            //如果连接对象关闭,则打开连接
            if (Conn.State.Equals(ConnectionState.Closed))
                Conn.Open();
            //如果连接中断,则重启连接
            if (Conn.State.Equals(ConnectionState.Broken))
            {
                Conn.Close();
                Conn.Open();
            }
        }

        /// <summary>
        /// 查询、获取 DataReader
        /// </summary>
        /// <param name="sqlStr">Sql语句</param>
        /// <returns>SqlDataReader 数据集</returns>
        public SqlDataReader GetDataReader(string sqlStr)
        {
            InitConnection();
            SqlCommand cmd = new SqlCommand(sqlStr, Conn);
            //CommandBehavior.CloseConnection 命令行为:当 DataReader 对象被关闭时,自动关闭占用的连接对象
            return cmd.ExecuteReader(CommandBehavior.CloseConnection);
        }

        /// <summary>
        /// 查询、获取 DataTable
        /// </summary>
        /// <param name="sqlStr">Sql语句</param>
        /// <returns>DataTable 数据表</returns>
        public DataSet GetDataSet(string sqlStr)
        {
            InitConnection();
            DataSet ds = new DataSet();
            SqlDataAdapter dap = new SqlDataAdapter(sqlStr, Conn);
            dap.Fill(ds);
            Conn.Close();
            return ds;
        }

        /// <summary>
        /// 执行增删改
        /// </summary>
        /// <param name="sqlStr">Sql语句</param>
        /// <returns>受影响行数</returns>
        public int ExecuteNonQuery(string sqlStr)
        {
            InitConnection();
            SqlCommand cmd = new SqlCommand(sqlStr, Conn);
            int rows = cmd.ExecuteNonQuery();
            Conn.Close();
            return rows;
        }

        /// <summary>
        /// 执行聚合函数
        /// </summary>
        /// <param name="sqlStr">Sql语句</param>
        /// <returns>Object对象</returns>
        public object ExecuteScalar(string sqlStr)
        {
            InitConnection();
            SqlCommand cmd = new SqlCommand(sqlStr, Conn);
            object result = cmd.ExecuteScalar();
            Conn.Close();
            return result;
        }
    }
}

这是一篇过去很久的文章,其中的信息可能已经有所发展或是发生改变。

本文作者:她和她的猫
本文地址https://her-cat.com/posts/2016/11/03/dbhelper-class/
版权声明:本博客所有文章除特别声明外,均采用 CC BY-NC-SA 4.0 许可协议。转载请注明出处!