国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 開發 > 綜合 > 正文

SQLHelper

2024-07-21 02:48:34
字體:
來源:轉載
供稿:網友
SQLHelper
using System;using System.Configuration;using System.Data;using System.Data.SqlClient;using System.Collections;namespace Maticsoft.DBUtility{    /// <summary>    /// The SqlHelper class is intended to encapsulate high performance,     /// scalable best PRactices for common uses of SqlClient.    /// </summary>    public abstract class SqlHelper    {        //Database connection strings        public static readonly string ConnectionStringLocalTransaction =  ConfigurationManager.AppSettings["SQLConnString1"];        public static readonly string ConnectionStringInventoryDistributedTransaction =  ConfigurationManager.AppSettings["SQLConnString2"];        public static readonly string ConnectionStringOrderDistributedTransaction =  ConfigurationManager.AppSettings["SQLConnString3"];        public static readonly string ConnectionStringProfile =  ConfigurationManager.AppSettings["SQLProfileConnString"];        // Hashtable to store cached parameters        private static Hashtable parmCache = Hashtable.Synchronized(new Hashtable());        /// <summary>        /// Execute a SqlCommand (that returns no resultset) against the database specified in the connection string         /// using the provided parameters.        /// </summary>        /// <remarks>        /// e.g.:          ///  int result = ExecuteNonQuery(connString, CommandType.StoredProcedure, "PublishOrders", new SqlParameter("@prodid", 24));        /// </remarks>        /// <param name="connectionString">a valid connection string for a SqlConnection</param>        /// <param name="commandType">the CommandType (stored procedure, text, etc.)</param>        /// <param name="commandText">the stored procedure name or T-SQL command</param>        /// <param name="commandParameters">an array of SqlParamters used to execute the command</param>        /// <returns>an int representing the number of rows affected by the command</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>        /// Execute a SqlCommand (that returns no resultset) against an existing database connection         /// using the provided parameters.        /// </summary>        /// <remarks>        /// e.g.:          ///  int result = ExecuteNonQuery(connString, CommandType.StoredProcedure, "PublishOrders", new SqlParameter("@prodid", 24));        /// </remarks>        /// <param name="conn">an existing database connection</param>        /// <param name="commandType">the CommandType (stored procedure, text, etc.)</param>        /// <param name="commandText">the stored procedure name or T-SQL command</param>        /// <param name="commandParameters">an array of SqlParamters used to execute the command</param>        /// <returns>an int representing the number of rows affected by the command</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>        /// Execute a SqlCommand (that returns no resultset) using an existing SQL Transaction         /// using the provided parameters.        /// </summary>        /// <remarks>        /// e.g.:          ///  int result = ExecuteNonQuery(connString, CommandType.StoredProcedure, "PublishOrders", new SqlParameter("@prodid", 24));        /// </remarks>        /// <param name="trans">an existing sql transaction</param>        /// <param name="commandType">the CommandType (stored procedure, text, etc.)</param>        /// <param name="commandText">the stored procedure name or T-SQL command</param>        /// <param name="commandParameters">an array of SqlParamters used to execute the command</param>        /// <returns>an int representing the number of rows affected by the command</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>        /// Execute a SqlCommand that returns a resultset against the database specified in the connection string         /// using the provided parameters.        /// </summary>        /// <remarks>        /// e.g.:          ///  SqlDataReader r = ExecuteReader(connString, CommandType.StoredProcedure, "PublishOrders", new SqlParameter("@prodid", 24));        /// </remarks>        /// <param name="connectionString">a valid connection string for a SqlConnection</param>        /// <param name="commandType">the CommandType (stored procedure, text, etc.)</param>        /// <param name="commandText">the stored procedure name or T-SQL command</param>        /// <param name="commandParameters">an array of SqlParamters used to execute the command</param>        /// <returns>A SqlDataReader containing the results</returns>        public static SqlDataReader ExecuteReader(string connectionString, CommandType cmdType, string cmdText, params SqlParameter[] commandParameters)        {            SqlCommand cmd = new SqlCommand();            SqlConnection conn = new SqlConnection(connectionString);            // we use a try/catch here because if the method throws an exception we want to             // close the connection throw code, because no datareader will exist, hence the             // commandBehaviour.CloseConnection will not work            try            {                PrepareCommand(cmd, conn, null, cmdType, cmdText, commandParameters);                SqlDataReader rdr = cmd.ExecuteReader(CommandBehavior.CloseConnection);                cmd.Parameters.Clear();                return rdr;            }            catch            {                conn.Close();                throw;            }        }        /// <summary>        /// Execute a SqlCommand that returns the first column of the first record against the database specified in the connection string         /// using the provided parameters.        /// </summary>        /// <remarks>        /// e.g.:          ///  Object obj = ExecuteScalar(connString, CommandType.StoredProcedure, "PublishOrders", new SqlParameter("@prodid", 24));        /// </remarks>        /// <param name="connectionString">a valid connection string for a SqlConnection</param>        /// <param name="commandType">the CommandType (stored procedure, text, etc.)</param>        /// <param name="commandText">the stored procedure name or T-SQL command</param>        /// <param name="commandParameters">an array of SqlParamters used to execute the command</param>        /// <returns>An object that should be converted to the expected type using Convert.To{Type}</returns>        public static object ExecuteScalar(
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 丹棱县| 福贡县| 佛学| 南丹县| 收藏| 石棉县| 金湖县| 锡林郭勒盟| 东兰县| 阿荣旗| 万年县| 城口县| 谢通门县| 玉山县| 黄浦区| 大庆市| 新巴尔虎右旗| 麦盖提县| 永胜县| 清远市| 汕头市| 开封市| 余姚市| 眉山市| 景谷| 广安市| 大田县| 精河县| 石台县| 若羌县| 永宁县| 旌德县| 桦南县| 巴马| 大同市| 沿河| 赤峰市| 增城市| 称多县| 筠连县| 河西区|