码迷,mamicode.com
首页 > 数据库 > 详细

SQLHELPER

时间:2017-05-06 01:09:21      阅读:209      评论:0      收藏:0      [点我收藏+]

标签:返回   end   pre   dal   blog   new   tco   sel   apt   

 

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

 

SQLHELPER

标签:返回   end   pre   dal   blog   new   tco   sel   apt   

原文地址:http://www.cnblogs.com/su1643/p/6815161.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!