SQLHELPER


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

namespace Jifang.Dal
{
    public class SqlHelper
    {
        private static readonly string connString = ConfigurationManager.ConnectionStrings["conStr"].ConnectionString;

        #region GetTable
        /// 
        /// 获取表格
        /// 
        /// 
        /// sql类型,sql语句或存储过程
        /// 参数集
        /// 
        public static DataTable GetDataTable(string sql, CommandType type, params SqlParameter[] pars)
        {
            using (SqlConnection conn = new SqlConnection(connString))
            {
                using (SqlDataAdapter myda = new SqlDataAdapter(sql, conn))
                {
                    myda.SelectCommand.CommandType = type;  //如果执行的是sql语句,则type为字符串,如果执行的是存储过程,则type为存储过程
                    if (pars != null)
                    {
                        myda.SelectCommand.Parameters.AddRange(pars);
                    }
                    DataTable dt = new DataTable();
                    myda.Fill(dt);
                    return dt;
                }
            }
        }
        #endregion

        #region Insert
        public static int ExcuteNonquery(string sql, CommandType type, params SqlParameter[] pars)
        {
            using (SqlConnection conn = new SqlConnection(connString))
            {
                using (SqlCommand cmd = new SqlCommand(sql, conn))
                {
                    cmd.CommandType = type;
                    if (pars != null)
                    {
                        cmd.Parameters.AddRange(pars);
                    }
                    conn.Open();
                    return cmd.ExecuteNonQuery();
                }
            }
        }
        #endregion

        #region Update
        public static int ExcuteUpdate(string sql, CommandType type, params SqlParameter[] pars)
        {
            using (SqlConnection conn = new SqlConnection(connString))
            {
                using (SqlCommand cmd = new SqlCommand(sql, conn))
                {
                    cmd.CommandType = type;
                    if (pars != null)
                    {
                        cmd.Parameters.AddRange(pars);
                    }
                    conn.Open();
                    return cmd.ExecuteNonQuery();
                }
            }
        }
    
        #endregion

        #region Delete
        public static int ExcuteDel(string sql, CommandType type, params SqlParameter[] pars)
        {
            using (SqlConnection conn = new SqlConnection(connString))
            {
                using (SqlCommand cmd = new SqlCommand(sql, conn))
                {
                    cmd.CommandType = type;
                    if (pars != null)
                    {
                        cmd.Parameters.AddRange(pars);
                    }
                    conn.Open();
                    return cmd.ExecuteNonQuery();
                }
            }
        }
        #endregion

        #region 返回结果集第一行第一列的值
        /// 
        /// 返回结果集第一行第一列的值
        /// 
        /// 
        /// 
        /// 
        /// 
        public static Object ExcuteScalare(string sql, CommandType type, params SqlParameter[] pars)
        {
            using (SqlConnection conn = new SqlConnection(connString))
            {
                using (SqlCommand cmd = new SqlCommand(sql, conn))
                {
                    cmd.CommandType = type;
                    if (pars != null)
                    {
                        cmd.Parameters.AddRange(pars);
                    }
                    conn.Open();
                    return cmd.ExecuteScalar();
                }
            }
        }
        #endregion
    }
}