C# SQL数据库的访问类

 /// <summary>
    /// SQL数据库的访问类
    /// </summary>
    public class SQLHelper
    {
        /// <summary>
        /// 执行非查询
        /// </summary>
        /// <param name="connectionString">连接字符串</param>
        /// <param name="cmdType">命令类型</param>
        /// <param name="cmdText">命令的文本</param>
        /// <param name="commandParameters">命令的参数</param>
        /// <returns>返回影响的结果集行数</returns>
        public static int ExecuteNonQuery(string connectionString, CommandType cmdType, string cmdText, params SqlParameter[] commandParameters)
        {

            SqlCommand cmd = new SqlCommand();

            using (SqlConnection conn = new SqlConnection(connectionString))
            {
                PrepareCommand(cmd, conn, null, cmdType, cmdText, commandParameters);
                int val = cmd.ExecuteNonQuery();
                cmd.Parameters.Clear();
                return val;
            }
        }
        /// <summary>
        /// 执行非查询
        /// </summary>
        /// <param name="connection">数据库连接</param>
        /// <param name="cmdType">命令的类型</param>
        /// <param name="cmdText">命令的文本</param>
        /// <param name="commandParameters">命令的参数</param>
        /// <returns>返回影响的结果集行数</returns>
        public static int ExecuteNonQuery(SqlConnection connection, CommandType cmdType, string cmdText, params SqlParameter[] commandParameters)
        {

            SqlCommand cmd = new SqlCommand();

            PrepareCommand(cmd, connection, null, cmdType, cmdText, commandParameters);
            int val = cmd.ExecuteNonQuery();
            cmd.Parameters.Clear();
            return val;
        }
        /// <summary>
        /// 执行非查询
        /// </summary>
        /// <param name="trans">事务</param>
        /// <param name="cmdType">命令的类型</param>
        /// <param name="cmdText">命令的文本</param>
        /// <param name="commandParameters">命令的参数</param>
        /// <returns>返回影响的结果集行数</returns>
        public static int ExecuteNonQuery(SqlTransaction trans, CommandType cmdType, string cmdText, params SqlParameter[] commandParameters)
        {
            SqlCommand cmd = new SqlCommand();
            PrepareCommand(cmd, trans.Connection, trans, cmdType, cmdText, commandParameters);
            int val = cmd.ExecuteNonQuery();
            cmd.Parameters.Clear();
            return val;
        }

        /// <summary>
        /// 执行查询
        /// </summary>
        /// <param name="connectionString">练级字符串</param>
        /// <param name="cmdType">命令的类型</param>
        /// <param name="cmdText">命令的文本</param>
        /// <param name="commandParameters">命令的参数数组</param>
        /// <returns>返回结果集</returns>
        public static SqlDataReader ExecuteReader(string connectionString, CommandType cmdType, string cmdText, params SqlParameter[] commandParameters)
        {
            SqlCommand cmd = new SqlCommand();
          
            SqlConnection conn = new SqlConnection(connectionString);
            try
            {
                PrepareCommand(cmd, conn, null, cmdType, cmdText, commandParameters);
                SqlDataReader rdr = cmd.ExecuteReader(CommandBehavior.CloseConnection);
                cmd.Parameters.Clear();
                return rdr;
            }
            catch
            {
                conn.Close();
                throw;
            }
        }

        /// <summary>
        /// 执行查询
        /// </summary>
        /// <param name="connectionString">练级字符串</param>
        /// <param name="cmdType">命令的类型</param>
        /// <param name="cmdText">命令的文本</param>
        /// <param name="commandParameters">命令的参数数组</param>
        /// <returns>返回结果集</returns>
        public static SqlDataReader ExecuteReader(SqlTransaction transaction, CommandType cmdType, string cmdText, params SqlParameter[] commandParameters)
        {
            SqlCommand cmd = new SqlCommand();
            try
            {
                PrepareCommand(cmd, transaction.Connection, transaction, cmdType, cmdText, commandParameters);
                SqlDataReader rdr = cmd.ExecuteReader(CommandBehavior.Default);
                cmd.Parameters.Clear();
                return rdr;
            }
            catch
            {                
                throw;
            }
        }

        /// <summary>
        /// 读取数据
        /// </summary>
        /// <param name="connectionString">数据库连接字符串</param>
        /// <param name="cmdType">数据命令类型</param>
        /// <param name="cmdText">数据命令的文本</param>
        /// <param name="cmd">需要被执行的数据命令</param>
        /// <param name="commandParameters">数据命令的参数</param>
        /// <returns>返回影响的结果集行数</returns>
        public static SqlDataReader ExecuteReader(string connectionString, CommandType cmdType, string cmdText, out SqlCommand cmd, params SqlParameter[] commandParameters)
        {
            cmd = new SqlCommand();
            SqlConnection conn = new SqlConnection(connectionString);
            try
            {
                PrepareCommand(cmd, conn, null, cmdType, cmdText, commandParameters);
                SqlDataReader rdr = cmd.ExecuteReader(CommandBehavior.CloseConnection);
                return rdr;
            }
            catch
            {
                conn.Close();
                throw;
            }
        }
        //匹配参数
        private static void PrepareCommand(SqlCommand cmd, SqlConnection conn, SqlTransaction trans, CommandType cmdType, string cmdText, SqlParameter[] cmdParms)
        {

            if (conn.State != ConnectionState.Open)
                conn.Open();

            cmd.Connection = conn;
            cmd.CommandText = cmdText;

            if (trans != null)
                cmd.Transaction = trans;

            cmd.CommandType = cmdType;

            if (cmdParms != null)
            {
                foreach (SqlParameter parm in cmdParms)
                    cmd.Parameters.Add(parm);
            }
        }

    }

 

posted @ 2016-12-07 15:08  涂山吕吕  阅读(2730)  评论(0编辑  收藏  举报