SQLHelper

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;


namespace Amwell.Utility
{
    public abstract class SqlHelper
    {
        private static string connStr = ConfigurationManager.ConnectionStrings["connStr"].ToString();//连接字符串


        #region 执行SQL语句
        /// <summary>
        /// 执行不带参数的更新SQL语句
        /// </summary>
        /// <param name="sql"></param>
        /// <returns></returns>
        public static int ExcuteNonSql(string sql)
        {
            using (SqlConnection conn = new SqlConnection(connStr))
            {
                SqlCommand cmd = new SqlCommand(sql, conn);
                conn.Open();
                return cmd.ExecuteNonQuery();
            }
        }


        /// <summary>
        /// 执行带参数的更新SQL语句
        /// </summary>
        /// <param name="sql"></param>
        /// <param name="paras"></param>
        /// <returns></returns>
        public static int ExcuteNonSql(string sql, params SqlParameter[] paras)
        {
            using (SqlConnection conn = new SqlConnection(connStr))
            {
                SqlCommand cmd = new SqlCommand(sql, conn);
                cmd.Parameters.AddRange(paras);
                conn.Open();
                return cmd.ExecuteNonQuery();
            }
        }


        /// <summary>
        /// 执行不带参数的查询语句,返回Table
        /// </summary>
        /// <param name="sql"></param>
        /// <returns></returns>
        public static DataTable ExcuteSelSql(string sql)
        {
            using (SqlDataAdapter ada = new SqlDataAdapter(sql, connStr))
            {
                DataTable dt = new DataTable();
                ada.Fill(dt);
                return dt;
            }
        }


        /// <summary>
        /// 执行带参数的查询语句,返回Table
        /// </summary>
        /// <param name="sql"></param>
        /// <param name="paras"></param>
        /// <returns></returns>
        public static DataTable ExcuteSelSql(string sql, params SqlParameter[] paras)
        {
            using (SqlDataAdapter ada = new SqlDataAdapter(sql, connStr))
            {
                DataTable dt = new DataTable();
                ada.SelectCommand.Parameters.AddRange(paras);
                ada.Fill(dt);
                return dt;
            }
        }


        /// <summary>
        /// 执行SQL语句获取单个值,不带参数
        /// </summary>
        /// <param name="sql"></param>
        /// <returns></returns>
        public static int ExcuteScaler(string sql)
        {
            using (SqlConnection conn = new SqlConnection(connStr))
            {
                SqlCommand cmd = new SqlCommand(sql, conn);
                conn.Open();
                return (int)cmd.ExecuteScalar();
            }
        }


        /// <summary>
        /// 执行SQL语句获取单个值,带参数
        /// </summary>
        /// <param name="sql"></param>
        /// <returns></returns>
        public static int ExcuteScaler(string sql, params SqlParameter[] paras)
        {
            using (SqlConnection conn = new SqlConnection())
            {
                SqlCommand cmd = new SqlCommand(sql, conn);
                cmd.Parameters.AddRange(paras);
                conn.Open();
                return (int)cmd.ExecuteScalar();
            }
        }
        #endregion


        #region 执行存储过程
        /// <summary>
        /// 执行不带参数的更新存储过程
        /// </summary>
        /// <param name="procName"></param>
        /// <returns></returns>
        public static int ExcuteNonProc(string procName)
        {
            using (SqlConnection conn = new SqlConnection(connStr))
            {
                SqlCommand cmd = new SqlCommand(procName, conn);
                cmd.CommandType = CommandType.StoredProcedure;
                conn.Open();
                return cmd.ExecuteNonQuery();
            }
        }


        /// <summary>
        /// 执行带参数的更新存储过程
        /// </summary>
        /// <param name="procName"></param>
        /// <param name="paras"></param>
        /// <returns></returns>
        public static int ExcuteNonProc(string procName, params SqlParameter[] paras)
        {
            using (SqlConnection conn = new SqlConnection(connStr))
            {
                SqlCommand cmd = new SqlCommand(procName, conn);
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.AddRange(paras);
                conn.Open();
                return cmd.ExecuteNonQuery();
            }
        }


        /// <summary>
        /// 执行不带参数的查询存储过程,返回DataTable
        /// </summary>
        /// <param name="procName"></param>
        /// <returns></returns>
        public static DataTable ExcuteSelProc(string procName)
        {
            using (SqlConnection conn = new SqlConnection(connStr))
            {
                SqlCommand cmd = new SqlCommand(procName, conn);
                cmd.CommandType = CommandType.StoredProcedure;


                SqlDataAdapter ada = new SqlDataAdapter(cmd);
                DataTable dt = new DataTable();
                ada.Fill(dt);
                return dt;
            }
        }


        /// <summary>
        /// 执行带参数查询存储过程,返回DataTable
        /// </summary>
        /// <param name="procName"></param>
        /// <param name="paras"></param>
        /// <returns></returns>
        public static DataTable ExcuteSelProc(string procName, params SqlParameter[] paras)
        {
            using (SqlConnection conn = new SqlConnection(connStr))
            {
                SqlCommand cmd = new SqlCommand(procName, conn);
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.AddRange(paras);


                SqlDataAdapter ada = new SqlDataAdapter(cmd);
                DataTable dt = new DataTable();
                ada.Fill(dt);
                return dt;
            }
        }
        #endregion






        /// <summary>
        /// 测试是否连接
        /// </summary>
        /// <returns></returns>
        public static bool IsConnected()
        {
            bool result = false;
            using (SqlConnection conn = new SqlConnection(connStr))
            {
                conn.Open();
                if (conn.State == ConnectionState.Open)
                {
                    result = true;
                }
            }


            return result;
        }
    }
}


你可能感兴趣的:(SQLHelper)