C#之SqlHelper

add name=ConnStr connectionString=Data Source=.;Initial Catalog=test;User ID=sa;Password=123456/ using System;using System.Configuration;using System.Data;using System.Data.SqlClient;using S...

<add name="ConnStr" connectionString="Data Source=.;Initial Catalog=test;User ID=sa;Password=123456"/>

 

using System;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Collections;

namespace MyTest.common
{
    public abstract class SqlHelper
    {
        public static readonly string ConnStr = ConfigurationManager.ConnectionStrings["ConnStr"].ConnectionString;
        
        private static Hashtable m_ParmCache = Hashtable.Synchronized(new Hashtable());
        
        /// <summary>
        /// depiction:SQL执行无返回结果的操作
        /// </summary>
        /// <param name="<connectionString>"><数据库链接串></param>
        /// <param name="<cmdType>"><执行SQL或存储过程 CommandType.Text|CommandType.StoredProcedure></param>
        /// <param name="<cmdText>"><执行SQL或存储过程名称></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>
        /// depiction:SQL执行无返回结果的操作
        /// </summary>
        /// <param name="<connection>"><数据库连接></param>
        /// <param name="<cmdType>"><执行SQL或存储过程 CommandType.Text|CommandType.StoredProcedure></param>
        /// <param name="<cmdText>"><执行SQL或存储过程名称></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>
        /// depiction:SQL执行无返回结果的操作(存在事务操作)
        /// </summary>
        /// <param name="<trans>"><事务></param>
        /// <param name="<cmdType>"><执行SQL或存储过程 CommandType.Text|CommandType.StoredProcedure></param>
        /// <param name="<cmdText>"><执行SQL或存储过程名称></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>
        /// depiction:SQL执行返回返回数据集SqlDataReader
        /// </summary>
        /// <param name="<connectionString>"><数据库连接串></param>
        /// <param name="<cmdType>"><执行SQL或存储过程 CommandType.Text|CommandType.StoredProcedure></param>
        /// <param name="<cmdText>"><执行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);

            try
            {
                PrepareCommand(cmd, conn, null, cmdType, cmdText, commandParameters);
                SqlDataReader rdr = cmd.ExecuteReader(CommandBehavior.CloseConnection);
                cmd.Parameters.Clear();
                return rdr;
            }
            catch
            {
                conn.Close();
                throw;
            }
        }

        /// <summary>
        /// depiction:SQL执行返回值
        /// </summary>
        /// <param name="<connectionString>"><数据库连接串></param>
        /// <param name="<cmdType>"><执行SQL或存储过程 CommandType.Text|CommandType.StoredProcedure></param>
        /// <param name="<cmdText>"><执行SQL或存储过程名称></param>
        /// <param name="<commandParameters>"><参数集></param>
        /// <returns>
        /// <返回数据集SqlDataReader>
        /// </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;
            }
        }
        /// <summary>
        /// depiction:SQL执行返回值
        /// </summary>
        /// <param name="<connection>"><数据库连接></param>
        /// <param name="<cmdType>"><执行SQL或存储过程 CommandType.Text|CommandType.StoredProcedure></param>
        /// <param name="<cmdText>"><执行SQL或存储过程名称></param>
        /// <param name="<commandParameters>"><参数集></param>
        /// <returns>
        /// <返回SQL执行结果>
        /// </returns>
        public static object ExecuteScalar(SqlConnection connection, CommandType cmdType, string cmdText, params SqlParameter[] commandParameters)
        {

            SqlCommand cmd = new SqlCommand();

            PrepareCommand(cmd, connection, null, cmdType, cmdText, commandParameters);
            object val = cmd.ExecuteScalar();
            cmd.Parameters.Clear();
            return val;
        }

        public static void CacheParameters(string cacheKey, params SqlParameter[] commandParameters)
        {
            m_ParmCache[cacheKey] = commandParameters;
        }

        /// <summary>
        /// depiction:SQL执行返回DataTable
        /// </summary>
        /// <param name="<connectionString>"><数据库连接串></param>
        /// <param name="<cmdType>"><执行SQL或存储过程 CommandType.Text|CommandType.StoredProcedure></param>
        /// <param name="<cmdText>"><执行SQL或存储过程名称></param>
        /// <param name="<commandParameters>"><参数集></param>
        /// <returns>
        /// <返回数据集DataTable>
        /// </returns>
        public static DataTable GetTable(string connectionString, CommandType cmdType, string cmdText, params SqlParameter[] commandParameters)
        {
            DataTable dt = new DataTable();
            SqlCommand cmd = new SqlCommand();
            using (SqlConnection conn = new SqlConnection(connectionString))
            {
                PrepareCommand(cmd, conn, null, cmdType, cmdText, commandParameters);
                SqlDataAdapter da = new SqlDataAdapter();
                da.SelectCommand = cmd;
                da.Fill(dt);
            }
            return dt;
        }
        /// <summary>
        /// depiction:SQL执行返回DataSet
        /// </summary>
        /// <param name="<connectionString>"><数据库连接串></param>
        /// <param name="<cmdType>"><执行SQL或存储过程 CommandType.Text|CommandType.StoredProcedure></param>
        /// <param name="<cmdText>"><执行SQL或存储过程名称></param>
        /// <param name="<commandParameters>"><参数集></param>
        /// <returns>
        /// <返回数据集DataSet>
        /// </returns>
        public static DataSet GetDataSet(string connectionString, CommandType cmdType, string cmdText, params SqlParameter[] commandParameters)
        {
            DataSet ds = new DataSet();
            SqlCommand cmd = new SqlCommand();
            using (SqlConnection conn = new SqlConnection(connectionString))
            {
                PrepareCommand(cmd, conn, null, cmdType, cmdText, commandParameters);
                SqlDataAdapter da = new SqlDataAdapter();
                da.SelectCommand = cmd;
                da.Fill(ds);
            }
            return ds;
        }

        public static SqlParameter[] GetCachedParameters(string cacheKey)
        {
            SqlParameter[] cachedParms = (SqlParameter[])m_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;
        }

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

        public static string ConnStr1 { get; set; }
    }
}

 

本文标题为:C#之SqlHelper

基础教程推荐