我的SqlHelper类!

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

namespace Dal
{
    public class SqlHelper
    {
        /// <summary>
        /// 连接字符串
        /// </summary>
        private static readonly string connStr = ConfigurationManager.ConnectionStrings["connStr"].ConnectionString;

        /// <summary>
        /// 执行Sql语句,返回影响行数(主要用:insert,update,delete)使用查找语句:返回-1
        /// </summary>
        /// <param name="sql"></param>
        /// <param name="cmdtype"></param>
        /// <param name="para"></param>
        /// <returns></returns>
        public static int ExecuteNonQuery(string sql, CommandType cmdtype, params SqlParameter[] para)
        {
            using (SqlConnection conn = new SqlConnection(connStr))
            {
                using (SqlCommand cmd = new SqlCommand(sql, conn))
                {
                    cmd.CommandType = cmdtype;
                    if (para != null)
                    {
                        cmd.Parameters.AddRange(para);
                    }
                    if (conn.State != ConnectionState.Open)
                    {
                        conn.Close();
                        conn.Open();
                    }
                    return cmd.ExecuteNonQuery();
                }
            }
        }
        /// <summary>
        /// 执行Sql语句,返回首行首列的值
        /// </summary>
        /// <param name="sql"></param>
        /// <param name="cmdtype"></param>
        /// <param name="para"></param>
        /// <returns></returns>
        public static object ExecuteScalar(string sql, CommandType cmdtype, params SqlParameter[] para)
        {
            using (SqlConnection conn = new SqlConnection(connStr))
            {
                using (SqlCommand cmd = new SqlCommand(sql, conn))
                {
                    cmd.CommandType = cmdtype;
                    if (para != null)
                    {
                        cmd.Parameters.AddRange(para);
                    }
                    if (conn.State != ConnectionState.Open)
                    {
                        conn.Close();
                        conn.Open();
                    }
                    return cmd.ExecuteScalar();
                }
            }
        }
        /// <summary>
        /// 执行sql语句,返回SqlDataReader
        /// </summary>
        /// <param name="sql"></param>
        /// <param name="cmdtype"></param>
        /// <param name="para"></param>
        /// <returns></returns>
        public static SqlDataReader ExecuteReader(string sql, CommandType cmdtype, params SqlParameter[] para)
        {
            SqlConnection conn = new SqlConnection(connStr);
            using (SqlCommand cmd = new SqlCommand(sql, conn))
            {
                cmd.CommandType = cmdtype;
                if (para != null)
                {
                    cmd.Parameters.AddRange(para);
                }
                try
                {
                    if (conn.State != ConnectionState.Open)
                    {
                        conn.Close();
                        conn.Open();
                    }
                    return cmd.ExecuteReader(CommandBehavior.CloseConnection);
                }
                catch
                {
                    conn.Close();
                    conn.Dispose();
                    throw;
                }

            }
        }

        public static DataTable ExecuteDataTable(string sql, CommandType cmdtype, params SqlParameter[] para)
        {
            using (SqlConnection conn = new SqlConnection(connStr))
            {
                using (SqlDataAdapter adt = new SqlDataAdapter(sql, conn))
                {
                    DataTable dt = new DataTable();
                    adt.SelectCommand.CommandType = cmdtype;
                    if (para != null)
                    {
                        adt.SelectCommand.Parameters.AddRange(para);
                    }
                    if (conn.State != ConnectionState.Open)
                    {
                        conn.Close();
                        conn.Open();
                    }
                    adt.Fill(dt);
                    return dt;

                }
            }
        }
    }
}

附上:WEB.CONFIG配置文件(数据库连接)
    <connectionStrings>
        <add name="Connstr" connectionString="Data Source=.;Initial Catalog=MALL;User Id=name;Password=pwd;" providerName="System.Data.SqlClient"/>
  </connectionStrings>

持续更新更新~~

posted @ 2016-10-11 21:12  拾诚  阅读(335)  评论(0编辑  收藏  举报