zoukankan      html  css  js  c++  java
  • 常用的SqlHelper类

    using System.Configuration;
    using System.Data;
    using System.Data.OleDb;
    
    namespace DAL
    {
        /// <summary>
        /// 数据库操作助手工具类
        /// 本类为抽象类不可以被实例化,需要的时候直接调用
        /// </summary>
        public abstract class DBHelper
        {
            //初始化字段
            private static OleDbConnection conn = null;
            private static OleDbCommand cmd = null;
    
            #region GetConn
            /// <summary>
            /// 获取数据库连接
            /// </summary>
            /// <returns>打开数据库连接</returns>
            private static OleDbConnection GetConn()
            {
                //从配置文件获取数据库连接字符串
                string connStr = ConfigurationManager.ConnectionStrings["DBPath"].ConnectionString;
                conn = new OleDbConnection(connStr);
                //判断数据库当前状态并选择是否打开连接
                if (conn.State == ConnectionState.Closed)
                {
                    conn.Open();
                }
                return conn;
            }
            #endregion
    
            #region ExecuteNonQuery
            /// <summary>
            /// 执行不带参数的增删改SQL语句或存储过程
            /// </summary>
            /// <param name="sql">传入的SQL语句</param>
            /// <returns>受影响行数</returns>
            public static int ExecuteNonQuery(string sql)
            {
                using (conn = GetConn())
                {
                    using (cmd = conn.CreateCommand())
                    {
                        cmd.CommandText = sql;
                        return cmd.ExecuteNonQuery();
                    }
                }
            }
            #endregion
    
            #region ExecuteNonQuery带参
            /// <summary>
            /// 执行带参数的增删改SQL语句或存储过程
            /// </summary>
            /// <param name="sql">传入的SQL语句</param>
            /// <param name="parameters">传入的参数</param>
            /// <returns>受影响行数</returns>
            public static int ExecuteNonQuery(string sql, params OleDbParameter[] parameters)
            {
                using (conn = GetConn())
                {
                    using (cmd = conn.CreateCommand())
                    {
                        cmd.CommandText = sql;
                        foreach (OleDbParameter p in parameters)
                        {
                            cmd.Parameters.Add(p);
                        }
                        return cmd.ExecuteNonQuery();
                    }
                }
            }
            #endregion
    
            #region ExecuteNonQuery带命令类型
            /// <summary>
            /// 执行不带参数的增删改SQL语句或存储过程
            /// </summary>
            /// <param name="sql">传入的SQL语句</param>
            /// <param name="ct">命令类型</param>
            /// <returns>受影响行数</returns>
            public static int ExecuteNonQuery(string sql, CommandType ct)
            {
                using (conn = GetConn())
                {
                    using (cmd = conn.CreateCommand())
                    {
                        cmd.CommandText = sql;
                        cmd.CommandType = ct;
                        return cmd.ExecuteNonQuery();
                    }
                }
            }
            #endregion
    
            #region ExecuteNonQuery带参和命令类型
            /// <summary>
            /// 执行带参数的增删改SQL语句或存储过程
            /// </summary>
            /// <param name="sql">传入的SQL语句</param>
            /// <param name="ct">命令类型</param>
            /// <param name="parameters">传入的参数</param>
            /// <returns>受影响行数</returns>
            public static int ExecuteNonQuery(string sql, CommandType ct, params OleDbParameter[] parameters)
            {
                using (conn = GetConn())
                {
                    using (cmd = conn.CreateCommand())
                    {
                        cmd.CommandText = sql;
                        cmd.CommandType = ct;
                        foreach (OleDbParameter p in parameters)
                        {
                            cmd.Parameters.Add(p);
                        }
                        return cmd.ExecuteNonQuery();
                    }
                }
            }
            #endregion
    
            #region ExecuteScalar
            /// <summary>
            /// 执行不带参数的SQL取数据表第一行第一列的值
            /// </summary>
            /// <param name="sql">传入的SQL语句</param>
            /// <returns>查询结果中第一行第一列的值</returns>
            public static object ExecuteScalar(string sql)
            {
                using (conn = GetConn())
                {
                    using (cmd = conn.CreateCommand())
                    {
                        cmd.CommandText = sql;
                        return cmd.ExecuteScalar();
                    }
                }
            }
            #endregion
    
            #region ExecuteScalar带参
            /// <summary>
            /// 执行带参数的SQL取数据表第一行第一列的值
            /// </summary>
            /// <param name="sql">传入的SQL语句</param>
            /// <param name="parameters">传入的参数</param>
            /// <returns>查询结果中第一行第一列的值</returns>
            public static object ExecuteScalar(string sql, params OleDbParameter[] parameters)
            {
                using (conn = GetConn())
                {
                    using (cmd = conn.CreateCommand())
                    {
                        cmd.CommandText = sql;
                        foreach (OleDbParameter p in parameters)
                        {
                            cmd.Parameters.Add(p);
                        }
                        return cmd.ExecuteScalar();
                    }
                }
            }
            #endregion
    
            #region ExecuteScalar带命令类型
            /// <summary>
            /// 执行不带参数的SQL取数据表第一行第一列的值
            /// </summary>
            /// <param name="sql">传入的SQL语句</param>
            /// <param name="ct">命令类型</param>
            /// <returns>查询结果中第一行第一列的值</returns>
            public static object ExecuteScalar(string sql, CommandType ct)
            {
                using (conn = GetConn())
                {
                    using (cmd = conn.CreateCommand())
                    {
                        cmd.CommandText = sql;
                        cmd.CommandType = ct;
                        return cmd.ExecuteScalar();
                    }
                }
            }
            #endregion
    
            #region ExecuteScalar带参和命令类型
            /// <summary>
            /// 执行带参数的SQL取数据表第一行第一列的值
            /// </summary>
            /// <param name="sql">传入的SQL语句</param>
            /// <param name="ct">命令类型</param>
            /// <param name="parameters">传入的参数</param>
            /// <returns>查询结果中第一行第一列的值</returns>
            public static object ExecuteScalar(string sql, CommandType ct, params OleDbParameter[] parameters)
            {
                using (conn = GetConn())
                {
                    using (cmd = conn.CreateCommand())
                    {
                        cmd.CommandText = sql;
                        cmd.CommandType = ct;
                        foreach (OleDbParameter p in parameters)
                        {
                            cmd.Parameters.Add(p);
                        }
                        return cmd.ExecuteScalar();
                    }
                }
            }
            #endregion
    
            #region ExecuteDataSet
            /// <summary>
            /// 执行不带参数的SQL取数据集
            /// </summary>
            /// <param name="sql">传入的SQL语句</param>
            /// <returns>数据集</returns>
            public static DataSet ExecuteDataSet(string sql)
            {
                using (conn = GetConn())
                {
                    using (cmd = conn.CreateCommand())
                    {
                        cmd.CommandText = sql;
                        DataSet dataset = new DataSet();
                        OleDbDataAdapter adapter = new OleDbDataAdapter(cmd);
                        adapter.Fill(dataset);
                        return dataset;
                    }
                }
            }
            #endregion
    
            #region ExecuteDataSet带参
            /// <summary>
            /// 执行带参数的SQL取数据集
            /// </summary>
            /// <param name="sql">传入的SQL语句</param>
            /// <param name="parameters">传入的参数</param>
            /// <returns>数据集</returns>
            public static DataSet ExecuteDataSet(string sql, params OleDbParameter[] parameters)
            {
                using (conn = GetConn())
                {
                    using (cmd = conn.CreateCommand())
                    {
                        cmd.CommandText = sql;
                        foreach (OleDbParameter p in parameters)
                        {
                            cmd.Parameters.Add(p);
                        }
                        DataSet dataset = new DataSet();
                        OleDbDataAdapter adapter = new OleDbDataAdapter(cmd);
                        adapter.Fill(dataset);
                        return dataset;
                    }
                }
            }
            #endregion
    
            #region ExecuteDataSet带命令类型
            /// <summary>
            /// 执行不带参数的SQL取数据集
            /// </summary>
            /// <param name="sql">传入的SQL语句</param>
            /// <param name="ct">命令类型</param>
            /// <returns>数据集</returns>
            public static DataSet ExecuteDataSet(string sql, CommandType ct)
            {
                using (conn = GetConn())
                {
                    using (cmd = conn.CreateCommand())
                    {
                        cmd.CommandText = sql;
                        cmd.CommandType = ct;
                        DataSet dataset = new DataSet();
                        OleDbDataAdapter adapter = new OleDbDataAdapter(cmd);
                        adapter.Fill(dataset);
                        return dataset;
                    }
                }
            }
            #endregion
    
            #region ExecuteDataSet带参和命令类型
            /// <summary>
            /// 执行带参数的SQL取数据集
            /// </summary>
            /// <param name="sql">传入的SQL语句</param>
            /// <param name="ct">命令类型</param>
            /// <param name="parameters">传入的参数</param>
            /// <returns>数据集</returns>
            public static DataSet ExecuteDataSet(string sql, CommandType ct, params OleDbParameter[] parameters)
            {
                using (conn = GetConn())
                {
                    using (cmd = conn.CreateCommand())
                    {
                        cmd.CommandText = sql;
                        cmd.CommandType = ct;
                        foreach (OleDbParameter p in parameters)
                        {
                            cmd.Parameters.Add(p);
                        }
                        DataSet dataset = new DataSet();
                        OleDbDataAdapter adapter = new OleDbDataAdapter(cmd);
                        adapter.Fill(dataset);
                        return dataset;
                    }
                }
            }
            #endregion
    
            #region ExecuteDataTable
            /// <summary>
            /// 执行不带参数的SQL取数据表
            /// </summary>
            /// <param name="sql">传入的SQL语句</param>
            /// <returns>数据表</returns>
            public static DataTable ExecuteDataTable(string sql)
            {
                using (conn = GetConn())
                {
                    using (cmd = conn.CreateCommand())
                    {
                        cmd.CommandText = sql;
                        DataSet dataset = new DataSet();
                        OleDbDataAdapter adapter = new OleDbDataAdapter(cmd);
                        adapter.Fill(dataset);
                        return dataset.Tables[0];
                    }
                }
            }
            #endregion
    
            #region ExecuteDataTable带参
            /// <summary>
            /// 执行带参数的SQL取数据表
            /// </summary>
            /// <param name="sql">传入的SQL语句</param>
            /// <param name="parameters">传入的参数</param>
            /// <returns>数据表</returns>
            public static DataTable ExecuteDataTable(string sql, params OleDbParameter[] parameters)
            {
                using (conn = GetConn())
                {
                    using (cmd = conn.CreateCommand())
                    {
                        cmd.CommandText = sql;
                        foreach (OleDbParameter p in parameters)
                        {
                            cmd.Parameters.Add(p);
                        }
                        DataSet dataset = new DataSet();
                        OleDbDataAdapter adapter = new OleDbDataAdapter(cmd);
                        adapter.Fill(dataset);
                        return dataset.Tables[0];
                    }
                }
            }
            #endregion
    
            #region ExecuteDataTable带命令类型
            /// <summary>
            /// 执行不带参数的SQL取数据表
            /// </summary>
            /// <param name="sql">传入的SQL语句</param>
            /// <param name="ct">命令类型</param>
            /// <returns>数据表</returns>
            public static DataTable ExecuteDataTable(string sql, CommandType ct)
            {
                using (conn = GetConn())
                {
                    using (cmd = conn.CreateCommand())
                    {
                        cmd.CommandText = sql;
                        cmd.CommandType = ct;
                        DataSet dataset = new DataSet();
                        OleDbDataAdapter adapter = new OleDbDataAdapter(cmd);
                        adapter.Fill(dataset);
                        return dataset.Tables[0];
                    }
                }
            }
            #endregion
    
            #region ExecuteDataTable带参和命令类型
            /// <summary>
            /// 执行带参数的SQL取数据表
            /// </summary>
            /// <param name="sql">传入的SQL语句</param>
            /// <param name="ct">命令类型</param>
            /// <param name="parameters">传入的参数</param>
            /// <returns>数据表</returns>
            public static DataTable ExecuteDataTable(string sql, CommandType ct, params OleDbParameter[] parameters)
            {
                using (conn = GetConn())
                {
                    using (cmd = conn.CreateCommand())
                    {
                        cmd.CommandText = sql;
                        cmd.CommandType = ct;
                        foreach (OleDbParameter p in parameters)
                        {
                            cmd.Parameters.Add(p);
                        }
                        DataSet dataset = new DataSet();
                        OleDbDataAdapter adapter = new OleDbDataAdapter(cmd);
                        adapter.Fill(dataset);
                        return dataset.Tables[0];
                    }
                }
            }
            #endregion
    
        }
    }
  • 相关阅读:
    Apache Ant自动化脚本
    使用Mybatis Generator结合Ant脚本快速自动生成Model、Mapper等文件的方法
    Maven Pom文件标签详解
    JAVA正则表达式语法大全
    RSA加密算法--Java实现详细案例:
    Java加密技术(一)——BASE64与单向加密算法MD5&SHA&MAC
    Base64算法的使用
    ElasticSearch使用IK中文分词---安装步骤记录
    ElasticSearch Java api 详解_V1.0
    HBase入门
  • 原文地址:https://www.cnblogs.com/icestone10/p/3250573.html
Copyright © 2011-2022 走看看