• SqlHelper 类


    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    //引入名称空间
    using System.Data;
    //DataSet
    using System.Data.SqlClient;
    //SqlParameter
    using System.Configuration;
    //web.config

    namespace DBSQLHelper
    {
        public class SQLHelper
        {
            //<connectionStrings>
            //<add name="connectionstring" connectionString="server=;uid=sa;pwd=sa;database="/>
            //</connectionStrings>

            /// <summary>
            /// 字符串的连接
            /// </summary>
            private readonly string connectionstring = ConfigurationManager.ConnectionStrings["connectionstring"].ConnectionString;

            /// <summary>
            /// 定义全局变量
            /// </summary>
            private SqlConnection connection;
            private SqlCommand command;
            private SqlDataAdapter dataadapter;

            /// <summary>
            /// 构造函数
            /// </summary>
            public SQLHelper() { }

            /// <summary>
            /// 释放资源
            /// </summary>
            public void Dispose()
            {
                //判断连接对象是否为空
                if (this.connection != null)
                {
                    //释放连接
                    this.connection.Dispose();
                    //设置连接对象为空
                    this.connection = null;
                }
            }

            /// <summary>
            /// 关闭
            /// </summary>
            public void Close()
            {
                //判断连接和连接状态
                if ((this.connection != null) && (this.connection.State == ConnectionState.Open))
                {
                    //关闭连接
                    this.connection.Close();
                }
            }

            /// <summary>
            /// 定义参数
            /// </summary>
            /// <param name="ParameterName">参数名字</param>
            /// <param name="DbType">参数类型</param>
            /// <param name="Size">参数大小</param>
            /// <param name="Direction">指向</param>
            /// <param name="Value">值</param>
            /// <returns>SqlParameter</returns>
            public SqlParameter CreateParameter(string ParameterName, SqlDbType DbType, int Size, ParameterDirection Direction, object Value)
            {
                //定义参数实例
                SqlParameter parameter;
                //判断是:eg:varchar(20)
                //如果是字符串类型的就要在后面定义size
                if (Size > 0)
                {
                    //构造函数
                    parameter = new SqlParameter(ParameterName, DbType, Size);
                }
                //eg:int
                //如果是整型就不需要在后面加上size
                else
                {
                    //构造函数
                    parameter = new SqlParameter(ParameterName, DbType);
                }
                //指向
                parameter.Direction = Direction;
                //input
                if ((Direction != ParameterDirection.Output) || (Value != null))
                {
                    parameter.Value = Value;
                }
                //返回参数实例
                return parameter;
            }

            /// <summary>
            /// 定义输入参数
            /// </summary>
            /// <param name="ParameterName">参数名字</param>
            /// <param name="DbType">参数类型</param>
            /// <param name="Size">参数大小</param>
            /// <param name="Value">值</param>
            /// <returns>SqlParameter</returns>
            public SqlParameter CreateInputParameter(string ParameterName, SqlDbType DbType, int Size, object Value)
            {
                //调用上面的参数方法
                //这个是指明指向是输入参数
                return this.CreateParameter(ParameterName, DbType, Size, ParameterDirection.Input, Value);
            }

            /// <summary>
            /// 定义输出参数
            /// </summary>
            /// <param name="ParameterName">参数名字</param>
            /// <param name="DbType">参数类型</param>
            /// <param name="Size">参数大小</param>
            /// <returns>SqlParameter</returns>
            public SqlParameter CreateOutputParameter(string ParameterName, SqlDbType DbType, int Size)
            {
                //调用上面的那个参数的方法
                //指向是输出没有value
                return this.CreateParameter(ParameterName, DbType, Size, ParameterDirection.Output, null);
            }

            /// <summary>
            /// 返回值
            /// </summary>
            /// <param name="ParameterName">参数名字</param>
            /// <param name="DbType">参数类型</param>
            /// <param name="Size">参数大小</param>
            /// <returns>SqlParameter</returns>
            public SqlParameter CreateReturnParameter(string ParameterName, SqlDbType DbType, int Size)
            {
                //这个指向既不是输入也不是输出,而是返回值
                return this.CreateParameter(ParameterName, DbType, Size, ParameterDirection.ReturnValue, null);
            }

            /// <summary>
            /// 返回影响的行数
            /// </summary>
            /// <param name="cmdText">执行的命令</param>
            /// <param name="cmdType">执行命令类型</param>
            /// <param name="parameter">参数</param>
            /// <returns>int</returns>
            public int ExecuteNonQuery(string cmdText, CommandType cmdType, params SqlParameter[] parameter)
            {
                using (connection = new SqlConnection(connectionstring))
                //实例化一个连接对象、调用连接对象里面的一个构造函数、设置连接字符串
                {
                    using (command = new SqlCommand(cmdText, connection))
                    //实例化一个命令对象、调用命令对象中的一个构造函数、第一个参数表示要执行的命令、第二个参数基于那个连接
                    {
                        try
                        {
                            connection.Open();
                            //打开连接(调用连接对象中的一个方法)
                            command.CommandType = cmdType;
                            //要执行的命令类型(纯文本、存储过程)
                            if (parameter != null)
                                //判断参数是否为空
                            {
                                //如果不为空、就将其遍历出来
                                foreach (SqlParameter par in parameter)
                                {
                                    //加载到要执行的命令中
                                    command.Parameters.Add(par);
                                }
                            }
                            //定义一个变量接收执行命令后、返回影响的行数(整型)
                            int count = command.ExecuteNonQuery();
                            //返回这个变量(对应上面定义方法时返回值的数据类型)
                            return count;
                        }
                            //抛出异常
                        catch (SqlException e)
                        {
                            throw new Exception(e.Message);
                        }
                    }
                }
            }

            /// <summary>
            /// 返回影响的行数
            /// </summary>
            /// <param name="cmdText">执行的命令</param>
            /// <param name="parameter">执行命令类型</param>
            /// <returns>int</returns>
            public int ExecuteNonQuery(string cmdText, SqlParameter[] parameter)
            {
                //这个方法可以看成是一个过渡的方法

                //调用上面定义的含有三个参数的方法
                //ExecuteNonQuery(string cmdText, CommandType cmdType, params SqlParameter[] parameter)
                //方法的参数必须实现
                //cmdText:执行命令
                //cmdType:命令的类型
                //parameter:是否含有参数

                //这个方法本身
                //带有两个参数
                //ExecuteNonQuery(string cmdText, SqlParameter[] parameter)
                //cmdText:要执行的命令
                //parameter:参数
                return ExecuteNonQuery(cmdText, CommandType.Text, parameter);         
            }

            public int ExecuteNonQuery(string cmdText)
            {
                //调用上面定义的含有两个参数的方法
                //ExecuteNonQuery(string cmdText, SqlParameter[] parameter)
                //方法的参数必须实现
                //cmdText:执行命令
                //parameter:是否含有参数

                //这个方法本身
                //带有一个参数
                //ExecuteNonQuery(string cmdText)
                //cmdText:要执行的命令
                return ExecuteNonQuery(cmdText, null);
            }

            /// <summary>
            /// 返回查询的结果
            /// </summary>
            /// <param name="cmdText">执行的命令</param>
            /// <param name="cmdType">执行命令类型</param>
            /// <param name="parameter">参数</param>
            /// <returns>SqlDataReader</returns>
            public SqlDataReader ExecuteReader(string cmdText, CommandType cmdType, params SqlParameter[] parameter)
            {
                //不能用using
                //解释:当用using的时候在没返回查询结果之前已经释放资源了
                connection = new SqlConnection(connectionstring);
                try
                {
                    connection.Open();
                    command = new SqlCommand(cmdText, connection);
                    command.CommandType = cmdType;
                    if (parameter != null)
                    {
                        command.Parameters.AddRange(parameter);
                    }
                    //command.ExecuteReader(CommandBehavior.CloseConnection);
                    //调用这个方法,这个方法有一个参数
                    //作用就是执行完之后释放资源
                    SqlDataReader datareader = command.ExecuteReader(CommandBehavior.CloseConnection);               
                    return datareader;
                }
                catch (SqlException e)
                {
                    throw new Exception(e.Message);
                }
            }

            public SqlDataReader ExecuteReader(string cmdText, SqlParameter[] parameter)
            {
                return ExecuteReader(cmdText, CommandType.Text, parameter);
            }

            public SqlDataReader ExecuteReader(string cmdText)
            {
                return ExecuteReader(cmdText, null);
            }

            /// <summary>
            /// 返回首行首列
            /// </summary>
            /// <param name="cmdText">执行的命令</param>
            /// <param name="cmdType">执行命令类型</param>
            /// <param name="parameter">参数</param>
            /// <returns>object</returns>
            public object ExecuteScalar(string cmdText, CommandType cmdType, params SqlParameter[] parameter)
            {
                using (connection = new SqlConnection(connectionstring))
                {
                    using (command = new SqlCommand(cmdText, connection))
                    {
                        try
                        {
                            connection.Open();
                            command.CommandType = cmdType;
                            if (parameter != null)
                            {
                                foreach (SqlParameter par in parameter)
                                {
                                    command.Parameters.Add(par);
                                }
                            }

                            object obj = command.ExecuteScalar(); 
                        
                            if ((Object.Equals(obj, null)) || (Object.Equals(obj, System.DBNull.Value)))
                            {
                                return null;
                            }
                            else
                            {
                                return obj;
                            }                       
                        }
                        catch (SqlException e)
                        {
                            throw new Exception(e.Message);
                        }
                    }
                }
            }

            public object ExecuteScalar(string cmdText, SqlParameter[] parameter)
            {
                return ExecuteScalar(cmdText, CommandType.Text, parameter);
            }

            public object ExecuteScalar(string cmdText)
            {
                return ExecuteScalar(cmdText, null);
            }

            /// <summary>
            /// 查询结果集
            /// </summary>
            /// <param name="cmdText">执行的命令</param>
            /// <param name="cmdType">执行命令类型</param>
            /// <param name="parameter">参数</param>
            /// <returns>DataSet</returns>
            public DataSet GetDataToDataSet(string cmdText, CommandType cmdType, params SqlParameter[] parameter)
            {
                using (connection = new SqlConnection(connectionstring))
                {
                    try
                    {
                        //适配器
                        //用适配器就不用打开连接,相应的打开已经被封装到这个对象里面了
                        SqlDataAdapter dataadapter = new SqlDataAdapter(cmdText, connection);
                        dataadapter.SelectCommand.CommandType = cmdType;
                        DataSet dataset = new DataSet();
                        if (parameter != null)
                        {
                            dataadapter.SelectCommand.Parameters.AddRange(parameter);
                        }
                        dataadapter.Fill(dataset);
                        return dataset;
                    }
                    catch (SqlException e)
                    {
                        throw new Exception(e.Message);
                    }
                }
            }

            public DataSet GetDataToDataSet(string cmdText, SqlParameter[] parameter)
            {
                return GetDataToDataSet(cmdText, CommandType.Text, parameter);
            }

            public DataSet GetDataToDataSet(string cmdText)
            {
                return GetDataToDataSet(cmdText, null);
            }

            public DataTable GetDataToTable(string cmdText, CommandType cmdType, params SqlParameter[] parameter)
            {
                using (connection = new SqlConnection(connectionstring))
                {
                    try
                    {
                        SqlDataAdapter dataadapter = new SqlDataAdapter(cmdText, connection);
                        dataadapter.SelectCommand.CommandType = cmdType;
                        DataTable datatable = new DataTable();
                        if (parameter != null)
                        {
                            dataadapter.SelectCommand.Parameters.AddRange(parameter);
                        }
                        dataadapter.Fill(datatable);
                        return datatable;
                    }
                    catch (SqlException e)
                    {
                        throw new Exception(e.Message);
                    }
                }
            }

            public DataTable GetDataToTable(string cmdText, SqlParameter[] parameter)
            {
                return GetDataToTable(cmdText, CommandType.Text, parameter);
            }

            public DataTable GetDataToTable(string cmdText)
            {
                return GetDataToTable(cmdText, null);
            }
        }
    }

  • 相关阅读:
    windows 7鼠标右键另存为没有桌面选项
    我心目中的Asp.net核心对象
    谈谈C# 4.0新特性“缺省参数”的实现
    C#正则表达式
    Request[]与Request.Params[]
    智力题
    SQL Server类型与C#类型对应关系
    UPdatepanel 的 优点 缺点
    细说 Request[]与Request.Params[]
    display:block 前后会换行
  • 原文地址:https://www.cnblogs.com/meroselove/p/1903413.html
Copyright © 2020-2023  润新知