• DbHelper第三版, 数据库通吃


    using System;
    using System.Collections;
    using System.Data;
    using System.Data.Common;
    using System.Configuration;


    namespace Lee.Data
    {
        public class DataAccess
        {
            #region 字段 构造函数
            public readonly DbProviderFactory Factory;
            public readonly ConnectionStringSettings ConnectionSettings;

            public static DataAccess Instance = new DataAccess();

            DataAccess() {
                this.ConnectionSettings = ConfigurationManager.ConnectionStrings["default"];
                this.Factory = DbProviderFactories.GetFactory(this.ConnectionSettings.ProviderName);
            }
            #endregion


            #region 创建 DbConnection, DbCommand对象
            /// <summary>
            /// 获取一个新的DbConnection对象
            /// </summary>
            /// <returns>DbConnection</returns>
            public DbConnection CreateConnection() {
                DbConnection connection = Factory.CreateConnection();
                connection.ConnectionString = this.ConnectionSettings.ConnectionString;
                return connection;
            }

            /// <summary>
            /// 使用sql语句创建一个DbCommand对象
            /// </summary>
            /// <param name="sql">sql语句</param>
            /// <returns></returns>
            public DbCommand CreateCommand(String sql) {
                return this.CreateCommand(sql, CommandType.Text, null, this.CreateConnection());
            }

            /// <summary>
            /// 使用sql语句,DbConnection对象创建一个DbCommand对象
            /// </summary>
            /// <param name="sql">sql语句</param>
            /// <param name="connection">DbConnection对象</param>
            /// <returns></returns>
            public DbCommand CreateCommand(String sql, DbConnection connection) {
                return this.CreateCommand(sql, CommandType.Text, null, connection);
            }

            /// <summary>
            /// 使用sql语句, CommandType, DbParameter数组 创建一个DbCommand对象
            /// </summary>
            /// <param name="sql">sql语句</param>
            /// <param name="type">DbCommand所执行命令的类型</param>
            /// <param name="parameters">DbCommand的参数</param>
            /// <returns></returns>
            public DbCommand CreateCommand(String sql, CommandType type, DbParameter[] parameters) {
                DbCommand cmd = Factory.CreateCommand();
                cmd.CommandType = type;
                cmd.CommandText = sql;
                cmd.Connection = this.CreateConnection();
                if(parameters != null && parameters.Length > 0)
                    cmd.Parameters.AddRange(parameters);
                return cmd;
            }

            /// <summary>
            /// 使用sql语句, CommandType, DbParameter数组, DbConnection对象 创建一个DbCommand对象
            /// </summary>
            /// <param name="sql">sql语句</param>
            /// <param name="type">DbCommand所执行命令的类型</param>
            /// <param name="parameters">DbCommand的参数</param>
            /// <param name="connection">connection对象</param>
            /// <returns></returns>
            public DbCommand CreateCommand(String sql, CommandType type, DbParameter[] parameters, DbConnection connection) {
                DbCommand cmd = null;
                if (connection == null) cmd = this.CreateConnection().CreateCommand();
                else cmd = connection.CreateCommand();
                cmd.CommandType = type;
                cmd.CommandText = sql;
                if (parameters != null && parameters.Length > 0)
                    cmd.Parameters.AddRange(parameters);
                return cmd;
            }
            #endregion


            #region 执行sql语句
            /// <summary>
            /// 执行sql语句返回受影响的行数
            /// </summary>
            /// <param name="sql">要执行的sql语句</param>
            /// <returns></returns>
            public int ExecuteNoneQuery(String sql) {
                using (DbConnection connection = this.CreateConnection()) {
                    using (DbCommand command = connection.CreateCommand()) {
                        this.Open(connection);
                        command.CommandText = sql;
                        return command.ExecuteNonQuery();
                    }
                }
            }

            /// <summary>
            /// 使用指定的DbConnection对象执行sql语句并返回受影响的行数
            /// </summary>
            /// <param name="sql">要执行的sql语句</param>
            /// <param name="connection">DbConnection对象</param>
            /// <returns></returns>
            public int ExecuteNoneQuery(String sql, DbConnection connection) {
                using (DbCommand command = connection.CreateCommand()) {
                    command.CommandText = sql;
                    this.Open(connection);
                    return command.ExecuteNonQuery();
                }
            }

            /// <summary>
            /// 执行sql语句获取第一行第一列的值 (Object类型)
            /// </summary>
            /// <param name="sql">要执行的sql语句</param>
            /// <returns></returns>
            public Object ExecuteScalar(String sql) {
                using (DbConnection connection = this.CreateConnection()) {
                    using (DbCommand command = connection.CreateCommand()) {
                        command.CommandText = sql;
                        this.Open(connection);
                        return command.ExecuteScalar();
                    }
                }
            }

            /// <summary>
            /// 使用指定的DbConnection对象执行sql语句并获取第一行第一列的值 (Object类型)
            /// </summary>
            /// <param name="sql">要执行的sql语句</param>
            /// <param name="connection">DbConnection对象</param>
            /// <returns></returns>
            public Object ExecuteScalar(String sql, DbConnection connection) {
                using (DbCommand command = connection.CreateCommand()) {
                    command.CommandText = sql;
                    command.Connection = connection;
                    this.Open(connection);
                    return command.ExecuteScalar();
                }
            }

            /// <summary>
            /// 执行sql语句获取第一行第一列的值 (int类型)
            /// </summary>
            /// <param name="sql">要执行的sql语句</param>
            /// <returns></returns>
            public Int32? ExecuteScalarGetInt(String sql) {
                Object obj =  this.ExecuteScalar(sql);
                return obj == null ? null : (Int32?)obj;
            }

            /// <summary>
            /// 使用指定的DbConnection执行sql语句并获取第一行第一列的值 (int类型)
            /// </summary>
            /// <param name="sql">要执行的sql语句</param>
            /// <param name="connection">DbConnection对象</param>
            /// <returns></returns>
            public Int32? ExecuteScalarGetInt(String sql, DbConnection connection) {
                Object obj = this.ExecuteScalar(sql, connection);
                return obj == null ? null : (Int32?)obj;
            }

            /// <summary>
            /// 执行sql语句并获取第一行第一列的值 (String类型)
            /// </summary>
            /// <param name="sql">要执行的sql语句</param>
            /// <returns></returns>
            public String ExcuteScalarGetString(String sql) {
                Object obj = this.ExecuteScalar(sql);
                return obj == null ? null : obj.ToString();
            }

            /// <summary>
            /// 使用指定的DbConnection执行sql语句并获取第一行第一列的值 (String类型)
            /// </summary>
            /// <param name="sql">要执行的sql语句</param>
            /// <param name="connection">DbConnection对象</param>
            /// <returns></returns>
            public String ExcuteScalarGetString(String sql, DbConnection connection) {
                Object obj = this.ExecuteScalar(sql, connection);
                return obj == null ? null : obj.ToString();
            }

            /// <summary>
            /// 使用指定的sql语句创建一个DbDataReader对象
            /// </summary>
            /// <param name="sql">sql语句</param>
            /// <returns></returns>
            public DbDataReader ExecuteReader(String sql) {
                DbConnection connection = this.CreateConnection();
                DbCommand command = connection.CreateCommand();
                command.CommandText = sql;
                this.Open(connection);
                return command.ExecuteReader(CommandBehavior.CloseConnection);
            }

            /// <summary>
            /// 使用指定的DbConnection对象执行sql语句并获取一个DbDataReader对象
            /// </summary>
            /// <param name="sql">sql语句</param>
            /// <param name="connection">DbConnection对象</param>
            /// <returns></returns>
            public DbDataReader ExecuteReader(String sql, DbConnection connection) {
                DbCommand command = connection.CreateCommand();
                command.CommandText = sql;
                this.Open(connection);
                return command.ExecuteReader();
            }

            /// <summary>
            /// 执行sql语句返回DataTable
            /// </summary>
            /// <param name="sql">sql语句</param>
            /// <returns></returns>
            public DataTable GetDataTable(String sql) {
                using (DbConnection connection = this.CreateConnection()) {
                    using (DbDataAdapter adapter = Factory.CreateDataAdapter()) {
                        using (adapter.SelectCommand = connection.CreateCommand()) {
                            adapter.SelectCommand.CommandText = sql;
                            DataTable table = new DataTable();
                            adapter.Fill(table);
                            return table;
                        }
                    }
                }
            }

            /// <summary>
            /// 执行sql语句返回DataTable
            /// </summary>
            /// <param name="sql"></param>
            /// <param name="connection"></param>
            /// <returns></returns>
            public DataTable GetDataTable(String sql, DbConnection connection) {
                using (DbDataAdapter adapter = Factory.CreateDataAdapter()) {
                    using (adapter.SelectCommand = connection.CreateCommand()) {
                        adapter.SelectCommand.CommandText = sql;
                        DataTable table = new DataTable();
                        adapter.Fill(table);
                        return table;
                    }
                }
            }

            #endregion


            #region 执行存储过程
            public ArrayList ExecuteProc(String procName, DbParameter[] parameters) {
                DbCommand command = this.CreateCommand(procName, CommandType.StoredProcedure, parameters);
                ArrayList values = new ArrayList(parameters.Length / 2);
                try {
                    this.Open(command);
                    command.ExecuteNonQuery();
                    foreach (DbParameter item in parameters)
                        if (item.Direction == ParameterDirection.Output)
                            values.Add(item.Value);
                }
                catch { throw; }
                finally { command.Connection.Dispose(); command.Dispose(); }
                return values;
            }

            /// <summary>
            /// 执行存储过程并将输出参数作为int数组返回
            /// </summary>
            /// <param name="procName">存储过程名称</param>
            /// <param name="parameters">存储过程参数</param>
            /// <returns></returns>
            public Int32[] ExecuteProcGetInt(String procName, DbParameter[] parameters) {
                ArrayList values = this.ExecuteProc(procName, parameters);
                Int32[] intValues = new Int32[values.Count];
                for (int i = 0; i < values.Count; i++)
                    intValues[i] = (Int32)values[i];
                return intValues;
            }

            /// <summary>
            /// 执行存储过程并将输出参数作为String数组返回
            /// </summary>
            /// <param name="procName">存储过程名称</param>
            /// <param name="parameters">存储过程参数</param>
            /// <returns></returns>
            public String[] ExecuteProcGetString(String procName, DbParameter[] parameters) {
                ArrayList values = this.ExecuteProc(procName, parameters);
                String[] stringValues = new String[values.Count];
                for (int i = 0; i < values.Count; i++)
                    stringValues[i] = values[i].ToString();
                return stringValues;
            }

            /// <summary>
            /// 执行存储过程并获取一个DbDataReader对象
            /// </summary>
            /// <param name="procName">存储过程名称</param>
            /// <param name="parameters">存储过程参数</param>
            /// <returns></returns>
            public DbDataReader ExecuteProcGetDataReader(String procName, DbParameter[] parameters) {
                DbCommand command = this.CreateCommand(procName, CommandType.StoredProcedure, parameters);
                DbDataReader reader = null;
                try { this.Open(command); reader = command.ExecuteReader(CommandBehavior.CloseConnection); }
                catch { throw; }
                //finally { command.Connection.Dispose(); command.Dispose(); }
                return reader;
            }

            /// <summary>
            /// 执行存储过程获取DataTable
            /// </summary>
            /// <param name="procName">存储过程名称</param>
            /// <param name="parameters">存储过程参数</param>
            /// <returns></returns>
            public DataTable ExecuteProcGetDataTable(String procName, DbParameter[] parameters) {
                DataTable table = new DataTable();
                DbDataAdapter adapter = null;

                try {
                    adapter = Factory.CreateDataAdapter();
                    adapter.SelectCommand = this.CreateCommand(procName, CommandType.StoredProcedure, parameters);
                    adapter.Fill(table);
                }
                catch { throw; }
                finally {
                    adapter.SelectCommand.Connection.Dispose();
                    adapter.SelectCommand.Dispose();
                    adapter.Dispose();
                }
                return table;
            }

            #endregion


            #region 其它

            /// <summary>
            /// 创建一个输入参数
            /// </summary>
            /// <param name="name"></param>
            /// <param name="value"></param>
            /// <returns></returns>
            public DbParameter CreateParameter(String name, Object value) {
                return this.CreateParameter(name, value, DbType.Object, ParameterDirection.Input);
            }

            /// <summary>
            /// 创建一个输入参数
            /// </summary>
            /// <param name="name"></param>
            /// <param name="value"></param>
            /// <param name="type"></param>
            /// <returns></returns>
            public DbParameter CreateParameter(String name, Object value, DbType type) {
                return this.CreateParameter(name, value, type, ParameterDirection.Input);
            }

            /// <summary>
            /// 创建一个输出参数
            /// </summary>
            /// <param name="name"></param>
            /// <returns></returns>
            public DbParameter CreateParameter(String name, DbType type) {
                DbParameter parameter = Factory.CreateParameter();
                parameter.ParameterName = name;
                parameter.Direction = ParameterDirection.Output;
                parameter.DbType = type;
                return parameter;
            }

            public DbParameter CreateParameter(String name, Object value, DbType type, ParameterDirection direct) {
                DbParameter parameter = Factory.CreateParameter();
                parameter.ParameterName = name;
                parameter.Value = value;
                parameter.DbType = type;
                parameter.Direction = direct;
                return parameter;
            }


            /// <summary>
            /// 打开连接
            /// </summary>
            /// <param name="connection">需要打开的DbConnection对象</param>
            public void Open(DbConnection connection) {
                if(connection.State != ConnectionState.Open)
                    try { connection.Open(); }
                    catch { throw; }
            }

            /// <summary>
            /// 打开连接
            /// </summary>
            /// <param name="command">需要打开的DbCommand对象</param>
            public void Open(DbCommand command) {
                if (command.Connection.State == ConnectionState.Open)
                    return;
                try { command.Connection.Open(); }
                catch { throw; }
            }
            #endregion
        }
    }

    佛为心,道为骨,儒为表,大度看世界; 技在手,能在身,思在脑,从容过生活; 三千年读史,不外功名利禄; 九万里悟道,终归诗酒田园;
  • 相关阅读:
    服务器修改时间
    修改MySQL时区
    记录一次服务器防火墙开放端口,参考了网上一位网友的方法可行,在此记录一下
    centos7.2放行80端口
    从零开始搭建系统3.4——缓存组件开发
    从零开始搭建系统3.3——图片服务开发及部署
    从零开始搭建系统3.2——微服务注册中心开发及部署
    从零开始搭搭建系统3.1——顶级pom制定
    从零开始搭建系统2.7——Quartz安装及配置
    从零开始搭建系统2.4——Jenkins安装及配置
  • 原文地址:https://www.cnblogs.com/taofx/p/4137675.html
Copyright © 2020-2023  润新知