zoukankan      html  css  js  c++  java
  • .Net SqlDbHelper

    using System.Configuration;
    using System.Data.SqlClient;
    using System.Data;
    
    namespace ExamDAL
    {
        class SqHelper
        {
            #region 属性区
    
            // 连接字符串
            private static string strConn;
    
            public static string StrConn
            {
                get
                {
                    return ConfigurationManager.ConnectionStrings["Exam"].ConnectionString;
                }
            }
    
            #endregion
    
            #region 方法区
    
            #region 获取受影响的行数
    
            /// 执行非查询存储过程和SQL语句
            /// 增、删、改
            /// </summary>
            /// <param name="strSQL">要执行的SQL语句</param>
            /// <param name="cmdType">Command类型</param>
            /// <param name="paras">参数列表,没有参数填入null</param>
            /// <returns>返回影响行数</returns>
            public static int ExcuteSQL(string strSQL, CommandType cmdType, params SqlParameter[] paras)
            {
                int count = 0;
                using (SqlConnection conn = new SqlConnection(StrConn))
                {
                    SqlCommand cmd = new SqlCommand(strSQL, conn);
                    cmd.CommandType = cmdType;
                    if (paras != null)
                    {
                        cmd.Parameters.AddRange(paras);
                    }
                    conn.Open();
                    count = cmd.ExecuteNonQuery();
                    conn.Close();
                }
                return count;
    
            }
    
            /// <summary>
            /// 执行没有参数的SQL的语句
            /// </summary>
            /// <param name="strSQL">执行SQL</param>
            /// <returns>受影响的行数</returns>
            public static int ExcuteSQL(string strSQL)
            {
                return ExcuteSQL(strSQL, null);
            }
    
            /// <summary>
            /// 执行有参数的SQL的语句
            /// </summary>
            /// <param name="strSQL">执行SQL</param>
            /// <param name="paras">参数</param>
            /// <returns>受影响的行数</returns>
            public static int ExcuteSQL(string strSQL, SqlParameter[] paras)
            {
                return ExcuteSQL(strSQL, CommandType.Text, paras);
            }
    
            /// <summary>
            /// 执行没有参数的存储过程
            /// </summary>
            /// <param name="ProcName">存储过程名称</param>
            /// <returns>受影响的行数</returns>
            public static int ExcuteProc(string ProcName)
            {
                return ExcuteSQL(ProcName, CommandType.StoredProcedure);
            }
    
            /// <summary>
            /// 执行有参数的存储过程
            /// </summary>
            /// <param name="ProcName">存储过程名称</param>
            /// <param name="pars">参数</param>
            /// <returns>受影响的行数</returns>
            public static int ExcuteProc(string ProcName, SqlParameter[] pars)
            {
                return ExcuteSQL(ProcName, CommandType.StoredProcedure, pars);
            }
    
            #endregion
    
            #region 获取第一行第一列的值
    
            /// <summary>
            /// 执行SQL语句,返回首行首列
            /// </summary>
            /// <param name="strSQL">要执行的SQL语句</param>
            /// <param name="cmdtype">执行的类型</param>
            /// <param name="paras">参数列表,没有参数填入null</param>
            /// <returns>返回的首行首列</returns>
            public static object GetObject(string strSQL, CommandType cmdtype, params SqlParameter[] paras)
            {
                object obj = null;
                using (SqlConnection conn = new SqlConnection(StrConn))
                {
                    SqlCommand cmd = new SqlCommand(strSQL, conn);
                    cmd.CommandType = cmdtype;
                    if (paras != null)
                    {
                        cmd.Parameters.AddRange(paras);
    
                    }
    
                    conn.Open();
                    obj = cmd.ExecuteScalar();
                    conn.Close();
                }
                return obj;
            }
    
            /// <summary>
            /// 根据sql语句获取单个值
            /// </summary>
            /// <param name="strSQL"></param>
            /// <returns></returns>
            public static object GetObject(string strSQL)
            {
                return GetObject(strSQL, null);
            }
    
            /// <summary>
            /// 根据sql语句 和 参数数组获取单个值
            /// </summary>
            /// <param name="strSQL"></param>
            /// <param name="paras"></param>
            /// <returns></returns>
            public static object GetObject(string strSQL, SqlParameter[] paras)
            {
                return GetObject(strSQL, CommandType.Text, paras);
            }
    
            /// <summary>
            /// 调用不带参数的存储过程获取单个值
            /// </summary>
            /// <param name="ProcName"></param>
            /// <returns></returns>
            public static object GetObjectByProc(string ProcName)
            {
                return GetObjectByProc(ProcName);
            }
    
            /// <summary>
            /// 调用带参数的存储过程获取单个值
            /// </summary>
            /// <param name="ProcName"></param>
            /// <param name="paras"></param>
            /// <returns></returns>
            public static object GetObjectByProc(string ProcName, params SqlParameter[] paras)
            {
                return GetObject(ProcName, CommandType.StoredProcedure, paras);
            }
    
            #endregion
    
            #region 获取DataReader
    
            /// <summary>
            /// 根据sql语句返回DataReader对象
            /// </summary>
            /// <param name="strSQL">sql语句</param>
            /// <returns>DataReader对象</returns>
            public static SqlDataReader GetReader(string strSQL)
            {
                return GetReader(strSQL, null);
            }
    
            /// <summary>
            /// 根据sql语句和参数返回DataReader对象
            /// </summary>
            /// <param name="strSQL">sql语句</param>
            /// <param name="paras">参数数组</param>
            /// <returns>DataReader对象</returns>
            public static SqlDataReader GetReader(string strSQL, params SqlParameter[] paras)
            {
                return GetReader(strSQL, CommandType.Text, paras);
            }
    
            /// <summary>
            /// 调用不带参数的存储过程,返回DataReader对象
            /// </summary>
            /// <param name="procName">存储过程名称</param>
            /// <returns>DataReader对象</returns>
            public static SqlDataReader GetReaderByProc(string procName)
            {
                return GetReaderByProc(procName);
            }
    
            /// <summary>
            /// 调用带有参数的存储过程,返回DataReader对象
            /// </summary>
            /// <param name="procName">存储过程名</param>
            /// <param name="paras">参数数组</param>
            /// <returns>DataReader对象</returns>
            public static SqlDataReader GetReaderByProc(string procName, params SqlParameter[] paras)
            {
                return GetReader(procName, CommandType.StoredProcedure, paras);
            }
    
            /// <summary>
            /// 查询SQL语句获取DataReader
            /// </summary>
            /// <param name="strSQL">查询的SQL语句</param>
            /// <param name="cmdtype">执行类型</param>
            /// <param name="paras">参数列表,没有参数填入null</param>
            /// <returns>查询到的DataReader(关闭该对象的时候,自动关闭连接)</returns>
            public static SqlDataReader GetReader(string strSQL, CommandType cmdtype, params SqlParameter[] paras)
            {
                SqlDataReader sqldr = null;
                SqlConnection conn = new SqlConnection(StrConn);
                SqlCommand cmd = new SqlCommand(strSQL, conn);
                cmd.CommandType = cmdtype;
                if (paras != null)
                {
                    cmd.Parameters.AddRange(paras);
                }
                conn.Open();
                //CommandBehavior.CloseConnection的作用是如果关联的DataReader对象关闭,则连接自动关闭
                sqldr = cmd.ExecuteReader(CommandBehavior.CloseConnection);
                return sqldr;
            }
    
            #endregion
    
            #region 获取DataTable
    
            /// <summary>
            /// 执行查询,返回DataTable对象
            /// </summary>
            /// <param name="strSQL">sql语句</param>
            /// <param name="cmdtype">Command类型</param>
            /// <param name="paras">参数数组</param>
            /// <returns>DataTable对象</returns>
            public static DataTable GetTable(string strSQL, CommandType cmdtype, params SqlParameter[] paras)
            {
                DataTable dt = new DataTable(); ;
                using (SqlConnection conn = new SqlConnection(StrConn))
                {
                    SqlDataAdapter da = new SqlDataAdapter(strSQL, conn);
                    da.SelectCommand.CommandType = cmdtype;
                    if (paras != null)
                    {
                        da.SelectCommand.Parameters.AddRange(paras);
                    }
                    da.Fill(dt);
                }
                return dt;
            }
    
            /// <summary>
            /// 执行无参数语句
            /// </summary>
            /// <param name="strSQL">执行SQL</param>
            /// <returns>DataTable</returns>
            public static DataTable GetTable(string strSQL)
            {
                return GetTable(strSQL, null);
            }
    
            /// <summary>
            /// 执行有参语句
            /// </summary>
            /// <param name="strSQL">执行SQL</param>
            /// <param name="paras">参数数组</param>
            /// <returns>DataTable</returns>
            public static DataTable GetTable(string strSQL, params SqlParameter[] paras)
            {
                return GetTable(strSQL, CommandType.Text, paras);
            }
    
            #endregion
    
            #region 获取DataSet
    
            /// <summary>
            /// 执行查询,返回DataSet对象
            /// </summary>
            /// <param name="strSQL">sql语句</param>
            /// <param name="cmdtype">Command类型</param>
            /// <param name="paras">参数数组</param>
            /// <returns>DataSet对象</returns>
            public static DataSet GetDataSet(string strSQL, CommandType cmdtype, params SqlParameter[] paras)
            {
                DataSet dt = new DataSet(); ;
                using (SqlConnection conn = new SqlConnection(StrConn))
                {
                    SqlDataAdapter da = new SqlDataAdapter(strSQL, conn);
                    da.SelectCommand.CommandType = cmdtype;
                    if (paras != null)
                    {
                        da.SelectCommand.Parameters.AddRange(paras);
                    }
                    da.Fill(dt);
                }
                return dt;
            }
    
            /// <summary>
            /// 执行无参数的SQL
            /// </summary>
            /// <param name="strSQL">执行SQL</param>
            /// <returns>DataSet</returns>
            public static DataSet GetDataSet(string strSQL)
            {
                return GetDataSet(strSQL, null);
            }
    
            /// <summary>
            /// 执行有参数的SQL
            /// </summary>
            /// <param name="strSQL">执行SQL</param>
            /// <param name="paras">参数数组</param>
            /// <returns>DataSet</returns>
            public static DataSet GetDataSet(string strSQL, SqlParameter[] paras)
            {
                return GetDataSet(strSQL, CommandType.Text, paras);
            }
    
            #endregion
    
            #region 批量插入数据处理
    
            /// <summary>
            /// 往数据库中批量插入数据
            /// </summary>
            /// <param name="sourceDt">数据源表</param>
            /// <param name="targetTable">服务器上目标表</param>
            public static void BulkToDB(DataTable sourceDt, string targetTable)
            {
                SqlConnection conn = new SqlConnection(strConn);
                SqlBulkCopy bulkCopy = new SqlBulkCopy(conn);   //用其它源的数据有效批量加载sql server表中
                bulkCopy.DestinationTableName = targetTable;    //服务器上目标表的名称
                bulkCopy.BatchSize = sourceDt.Rows.Count;   //每一批次中的行数
    
                try
                {
                    conn.Open();
                    if (sourceDt != null && sourceDt.Rows.Count != 0)
                        bulkCopy.WriteToServer(sourceDt);   //将提供的数据源中的所有行复制到目标表中
                }
                catch (System.Exception ex)
                {
                    throw ex;
                }
                finally
                {
                    conn.Close();
                    if (bulkCopy != null)
                        bulkCopy.Close();
                }
    
            }
    
            #endregion
    
            #endregion
        }
    }
  • 相关阅读:
    pgloader-pg迁移神器
    PostgreSQL备份工具-pg_probackup
    5、pgpool-II高可用性(一)数据库的高可用性
    4、pgpool-II 流复制模式
    3、pgpool-II 内置复制模式
    pgpool-II 的配置
    pgpool-II安装
    PG时间相减
    PostgreSQL 流复制解惑
    PostgreSQL改造非分区表为分区表
  • 原文地址:https://www.cnblogs.com/LoveQin/p/5355694.html
Copyright © 2011-2022 走看看