SQLHelper.cs

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

namespace WebApplication.DAL
{
    class SQLHelper
    {
        #region 通用方法

        // 數據鏈接池
        private SqlConnection con;

        /// <summary>
        /// 返回數據庫鏈接字符串
        /// </summary>
        /// <returns></returns>
        public static String GetSqlConnection()
        {
            String conn = ConfigurationManager.AppSettings["connectionString"];
            return conn;
        }

        #endregion

        #region 執行sql字符串

        /// <summary>
        /// 執行不帶參數的SQL語句
        /// </summary>
        /// <param name="Sqlstr">不帶參數的SQL語句</param>
        /// <returns>返回1表示執行成功</returns>
        public static int ExecuteSql(String Sqlstr)
        {
            String ConnStr = GetSqlConnection();
            using (SqlConnection conn = new SqlConnection(ConnStr))
            {
                SqlCommand cmd = new SqlCommand { Connection = conn, CommandText = Sqlstr };
                conn.Open();
                cmd.ExecuteNonQuery();
                conn.Close();
                return 1;
            }
        }

        /// <summary>
        /// 執行帶參數的SQL語句
        /// </summary>
        /// <param name="Sqlstr">SQL語句</param>
        /// <param name="param">參數對象數組</param>
        /// <returns>返回1表示執行成功</returns>
        public static int ExecuteSql(String Sqlstr, SqlParameter[] param)
        {
            String ConnStr = GetSqlConnection();
            using (SqlConnection conn = new SqlConnection(ConnStr))
            {
                SqlCommand cmd = new SqlCommand { Connection = conn, CommandText = Sqlstr };
                cmd.Parameters.AddRange(param);
                conn.Open();
                cmd.ExecuteNonQuery();
                conn.Close();
                return 1;
            }
        }

        /// <summary>
        /// 返回DataReader
        /// </summary>
        /// <param name="Sqlstr"></param>
        /// <returns></returns>
        public static SqlDataReader ExecuteReader(String Sqlstr)
        {
            String ConnStr = GetSqlConnection();
            SqlConnection conn = new SqlConnection(ConnStr);//返回DataReader時,是不能夠用using()的
            try
            {
                SqlCommand cmd = new SqlCommand { Connection = conn, CommandText = Sqlstr };
                conn.Open();
                return cmd.ExecuteReader(CommandBehavior.CloseConnection);//關閉關聯的Connection
            }
            catch 
            {
                return null;
            }
        }

        /// <summary>
        /// 執行SQL語句並返回數據表
        /// </summary>
        /// <param name="Sqlstr">SQL語句</param>
        /// <returns></returns>
        public static DataTable ExecuteDt(String Sqlstr)
        {
            String ConnStr = GetSqlConnection();
            using (SqlConnection conn = new SqlConnection(ConnStr))
            {
                SqlDataAdapter da = new SqlDataAdapter(Sqlstr, conn);
                DataTable dt = new DataTable();
                conn.Open();
                da.Fill(dt);
                conn.Close();
                return dt;
            }
        }

        /// <summary>
        /// 執行SQL語句並返回DataSet
        /// </summary>
        /// <param name="Sqlstr">SQL語句</param>
        /// <returns></returns>
        public static DataSet ExecuteDs(String Sqlstr)
        {
            String ConnStr = GetSqlConnection();
            using (SqlConnection conn = new SqlConnection(ConnStr))
            {
                SqlDataAdapter da = new SqlDataAdapter(Sqlstr, conn);
                DataSet ds = new DataSet();
                conn.Open();
                da.Fill(ds);
                conn.Close();
                return ds;
            }
        }

        #endregion

        #region 操做存儲過程

        /// <summary>
        /// 運行存儲過程(已重載)
        /// </summary>
        /// <param name="procName">存儲過程的名字</param>
        /// <returns>存儲過程的返回值</returns>
        public int RunProc(string procName)
        {
            SqlCommand cmd = CreateCommand(procName, null);
            cmd.ExecuteNonQuery();
            Close();
            return (int)cmd.Parameters["ReturnValue"].Value;
        }

        /// <summary>
        /// 運行存儲過程(已重載)
        /// </summary>
        /// <param name="procName">存儲過程的名字</param>
        /// <param name="prams">存儲過程的輸入參數列表</param>
        /// <returns>存儲過程的返回值</returns>
        public int RunProc(string procName, SqlParameter[] prams)
        {
            SqlCommand cmd = CreateCommand(procName, prams);
            cmd.ExecuteNonQuery();
            Close();
            return (int)cmd.Parameters[0].Value;
        }

        /// <summary>
        /// 運行存儲過程(已重載)
        /// </summary>
        /// <param name="procName">存儲過程的名字</param>
        /// <param name="dataReader">結果集</param>
        public void RunProc(string procName, out SqlDataReader dataReader)
        {
            SqlCommand cmd = CreateCommand(procName, null);
            dataReader = cmd.ExecuteReader(CommandBehavior.CloseConnection);
        }

        /// <summary>
        /// 運行存儲過程(已重載)
        /// </summary>
        /// <param name="procName">存儲過程的名字</param>
        /// <param name="prams">存儲過程的輸入參數列表</param>
        /// <param name="dataReader">結果集</param>
        public void RunProc(string procName, SqlParameter[] prams, out SqlDataReader dataReader)
        {
            SqlCommand cmd = CreateCommand(procName, prams);
            dataReader = cmd.ExecuteReader(CommandBehavior.CloseConnection);
        }

        /// <summary>
        /// 建立Command對象用於訪問存儲過程
        /// </summary>
        /// <param name="procName">存儲過程的名字</param>
        /// <param name="prams">存儲過程的輸入參數列表</param>
        /// <returns>Command對象</returns>
        private SqlCommand CreateCommand(string procName, SqlParameter[] prams)
        {
            // 肯定鏈接是打開的
            Open();
            SqlCommand cmd = new SqlCommand(procName, con) { CommandType = CommandType.StoredProcedure };
            // 添加存儲過程的輸入參數列表
            if (prams != null)
            {
                foreach (SqlParameter parameter in prams)
                    cmd.Parameters.Add(parameter);
            }
            // 返回Command對象
            return cmd;
        }

        /// <summary>
        /// 建立輸入參數
        /// </summary>
        /// <param name="ParamName">參數名</param>
        /// <param name="DbType">參數類型</param>
        /// <param name="Size">參數大小</param>
        /// <param name="Value">參數值</param>
        /// <returns>新參數對象</returns>
        public SqlParameter MakeInParam(string ParamName, SqlDbType DbType, int Size, object Value)
        {
            return MakeParam(ParamName, DbType, Size, ParameterDirection.Input, Value);
        }

        /// <summary>
        /// 建立輸出參數
        /// </summary>
        /// <param name="ParamName">參數名</param>
        /// <param name="DbType">參數類型</param>
        /// <param name="Size">參數大小</param>
        /// <returns>新參數對象</returns>
        public SqlParameter MakeOutParam(string ParamName, SqlDbType DbType, int Size)
        {
            return MakeParam(ParamName, DbType, Size, ParameterDirection.Output, null);
        }

        /// <summary>
        /// 建立存儲過程參數
        /// </summary>
        /// <param name="ParamName">參數名</param>
        /// <param name="DbType">參數類型</param>
        /// <param name="Size">參數大小</param>
        /// <param name="Direction">參數的方向(輸入/輸出)</param>
        /// <param name="Value">參數值</param>
        /// <returns>新參數對象</returns>
        public SqlParameter MakeParam(string ParamName, SqlDbType DbType, Int32 Size, ParameterDirection Direction, object Value)
        {
            SqlParameter param = Size > 0 ? new SqlParameter(ParamName, DbType, Size) : new SqlParameter(ParamName, DbType);
            param.Direction = Direction;
            if (!(Direction == ParameterDirection.Output && Value == null))
            {
                param.Value = Value;
            }
            return param;
        }

        #endregion

        #region 數據庫鏈接和關閉

        /// <summary>
        /// 打開鏈接池
        /// </summary>
        private void Open()
        {
            // 打開鏈接池
            if (con == null)
            {
                //這裏不只須要using System.Configuration;還要在引用目錄裏添加
                con = new SqlConnection(GetSqlConnection());
                con.Open();
            }
        }

        /// <summary>
        /// 關閉鏈接池
        /// </summary>
        public void Close()
        {
            if (con != null)
                con.Close();
        }

        /// <summary>
        /// 釋放鏈接池
        /// </summary>
        public void Dispose()
        {
            // 肯定鏈接已關閉
            if (con != null)
            {
                con.Dispose();
                con = null;
            }
        }

        #endregion
    }
}

網上找到的,留做不時之需。sql

相關文章
相關標籤/搜索