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
        /// <summary>
        /// 获取表格
        /// </summary>
        /// <param name="sql"></param>
        /// <param name="type">sql类型,sql语句或存储过程</param>
        /// <param name="pars">参数集</param>
        /// <returns></returns>
        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 返回结果集第一行第一列的值
        /// <summary>
        /// 返回结果集第一行第一列的值
        /// </summary>
        /// <param name="sql"></param>
        /// <param name="type"></param>
        /// <param name="pars"></param>
        /// <returns></returns>
        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
    }
}

 

posted @ 2017-05-05 22:22  *ち黑サカ  阅读(147)  评论(0编辑  收藏  举报