petshop 4.0的sqlhelper

sqlhelper的方法有一下几个

执行update,insert,delete的无结果集的的方法.当然根据参数不同,可重载出好几个方法例如:

View Code
/// <summary>

        /// 执行不返回结果集的SqlCommand,对数据库使用提供的参数中指定的连接字符串

        /// </summary>

        /// <remarks>

        /// e.g.:  

        ///  int result = ExecuteNonQuery(connString, CommandType.StoredProcedure, "PublishOrders", new SqlParameter("@prodid", 24));

        /// </remarks>

        /// <param name="connectionString">一个有效的类连接字符串</param>

        /// <param name="commandType">CommandType类型:存储过程,文本</param>

        /// <param name="commandText">存储过程名称或tsql命令</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关键字大括号内的连接自动关闭

            using (SqlConnection conn = new SqlConnection(connectionString))

            {   //准备命令

                PrepareCommand(cmd, conn, null, cmdType, cmdText, commandParameters);

                int val = cmd.ExecuteNonQuery();//执行命令

                cmd.Parameters.Clear();//清除命令参数

                return val;//返回影响行数

            }

        }



        /// <summary>

        /// 执行不返回结果集的SqlCommand,对数据库使用提供的参数中指定的连接对象

        /// </summary>

        /// <remarks>

        /// e.g.:  

        ///  int result = ExecuteNonQuery(connString, CommandType.StoredProcedure, "PublishOrders", new SqlParameter("@prodid", 24));

        /// </remarks>

        /// <param name="conn">现有的数据库连接对象</param>

        /// <param name="commandType">命令类型:存储过程或文本</param>

        /// <param name="commandText">存储过程名称或t-sql命令</param>

        /// <param name="commandParameters">用于执行命令的参数数组</param>

        /// <returns> int数据 ,表示类型的命令执行影响行数</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>

        /// 执行不返回结果集的SqlCommand,使用现有的参数现有的sql事务

        /// </summary>

        /// <remarks>

        /// e.g.:  

        ///  int result = ExecuteNonQuery(connString, CommandType.StoredProcedure, "PublishOrders", new SqlParameter("@prodid", 24));

        /// </remarks>

        /// <param name="trans">现有的sql事务</param>

        /// <param name="commandType">命令类型:存储过程或文本</param>

        /// <param name="commandText">存储过程或者t-sql命令</param>

        /// <param name="commandParameters">用于执行命令的参数数组</param>

        /// <returns>int 数据类型,表示执行事务的影响行数</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;

        }

当然这里面用了一个准备命令的方法也在这个类里面不过是自己类用,注意其访问修饰符

View Code
/// <summary>

        /// 准备用于执行的命令

        /// </summary>

        /// <param name="cmd">准备的sql命令</param>

        /// <param name="conn">sql连接对象</param>

        /// <param name="trans">sql事务</param>

        /// <param name="cmdType">sql命令类型</param>

        /// <param name="cmdText">sql命令语句或存储过程名称</param>

        /// <param name="cmdParms">参数</param>

        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);

            }

        }

这里只实现了返回结果集的方法.

View Code
 /// <summary>

        /// 执行返回结果集的sqlcommand,对数据库使用提供的参数中指定的连接字符串

        /// </summary>

        /// <remarks>

        /// e.g.:  

        ///  SqlDataReader r = ExecuteReader(connString, CommandType.StoredProcedure, "PublishOrders", new SqlParameter("@prodid", 24));

        /// </remarks>

        /// <param name="connectionString">一个有效的类连接字符串</param>

        /// <param name="commandType">命令类型:存储过程,文本等</param>

        /// <param name="commandText">存储过程或t-sql命令</param>

        /// <param name="commandParameters">用于执行命令的参数数组</param>

        /// <returns>返回结果集的SqlDataReader</returns>

        public static SqlDataReader ExecuteReader(string connectionString, CommandType cmdType, string cmdText, params SqlParameter[] commandParameters)

        {

            SqlCommand cmd = new SqlCommand();//创建命令对象

            SqlConnection conn = new SqlConnection(connectionString);//创建连接对象



            // 使用异常监控是因为cmd.ExecuteReader(CommandBehavior.CloseConnection)方法将sqldatareader的同时关闭数据连接

            try

            {

                PrepareCommand(cmd, conn, null, cmdType, cmdText, commandParameters);//准备命令

                SqlDataReader rdr = cmd.ExecuteReader(CommandBehavior.CloseConnection);//执行命令返回sqldatareader,在关闭datareader的同时关闭连接

                cmd.Parameters.Clear();

                return rdr;

            }

            catch

            {

                conn.Close();//发生异常强制关闭连接

                throw ;

            }

        }

当然还有返回首行首列的方法

View Code
/// <summary>

        /// 执行SqlCommand返回数据表的首行首列,对数据库使用提供的参数中指定的连接字符串

        /// </summary>

        /// <remarks>

        /// e.g.:  

        ///  Object obj = ExecuteScalar(connString, CommandType.StoredProcedure, "PublishOrders", new SqlParameter("@prodid", 24));

        /// </remarks>

        /// <param name="connectionString">一个有效的类连接字符串</param>

        /// <param name="commandType">命令类型:存储过程,文本等等</param>

        /// <param name="commandText">存储过程或t-sql命令</param>

        /// <param name="commandParameters">用于执行命令的参数数组</param>

        /// <returns>一个对象,应该使用类型转换来转换为需要的类型。</returns>

        public static object ExecuteScalar(string connectionString, CommandType cmdType, string cmdText, params SqlParameter[] commandParameters)

        {

            SqlCommand cmd = new SqlCommand();



            using (SqlConnection connection = new SqlConnection(connectionString))

            {

                PrepareCommand(cmd, connection, null, cmdType, cmdText, commandParameters);

                object val = cmd.ExecuteScalar();

                cmd.Parameters.Clear();

                return val;

            }

        }

里面的特殊方法,把sql参数添加到缓存,然后从缓存读取参数

View Code
//哈希表来存储缓存的参数

        private static Hashtable parmCache = Hashtable.Synchronized(new Hashtable());

        /// <summary>

        /// 参数数组添加到缓存

        /// </summary>

        /// <param name="cacheKey">参数缓存的关键字</param>

        /// <param name="cmdParms">缓存sqlparamters的数组</param>

        public static void CacheParameters(string cacheKey, params SqlParameter[] commandParameters)

        {

            parmCache[cacheKey] = commandParameters;

        }



        /// <summary>

        /// 读取缓存参数

        /// </summary>

        /// <param name="cacheKey">用于查找的键参数</param>

        /// <returns>缓存的参数数组</returns>

        public static SqlParameter[] GetCachedParameters(string cacheKey)

        {



            SqlParameter[] cachedParms = (SqlParameter[])parmCache[cacheKey];



            if (cachedParms == null)

                return null;



            SqlParameter[] clonedParms = new SqlParameter[cachedParms.Length];



            for (int i = 0, j = cachedParms.Length; i < j; i++)

                clonedParms[i] = (SqlParameter)((ICloneable)cachedParms[i]).Clone();



            return clonedParms;

        }

当然连接字符串是少不了的.

//Database connection strings 数据库连接字符串

        public static readonly string ConnectionStringLocalTransaction = ConfigurationManager.ConnectionStrings["SQLConnString1"].ConnectionString;

        public static readonly string ConnectionStringInventoryDistributedTransaction = ConfigurationManager.ConnectionStrings["SQLConnString2"].ConnectionString;

        public static readonly string ConnectionStringOrderDistributedTransaction = ConfigurationManager.ConnectionStrings["SQLConnString3"].ConnectionString;

        public static readonly string ConnectionStringProfile = ConfigurationManager.ConnectionStrings["SQLProfileConnString"].ConnectionString;

你可能感兴趣的:(help)