SqlHelper 基类

系统 1723 0
      
        using
      
      
         System;


      
      
        using
      
      
         System.Collections.Generic;


      
      
        using
      
      
         System.Linq;


      
      
        using
      
      
         System.Text;


      
      
        /**/
      
      
        using
      
      
         System.Data.SqlClient;


      
      
        using
      
      
         System.Data;


      
      
        using
      
      
         System.Configuration;


      
      
        /*
      
      
        *******************************************************************************

 ** 创建人:

 ** 创始时间:2012-11-27

 ** 修改人:

 ** 修改时间:

 ** 描述:

 ** 数据库操作基类

********************************************************************************
      
      
        */
      
      
        namespace
      
      
         DAL

{

    
      
      
        public
      
      
        class
      
      
         SqlHelper

    {

        
      
      
        public
      
      
        static
      
      
         SqlConnection connection;



        
      
      
        #region
      
       打开数据库

        
      
        ///
      
      
        <summary>
      
      
        ///
      
      
         打开数据库

        
      
      
        ///
      
      
        </summary>
      
      
        public
      
      
        static
      
      
         SqlConnection Conn

        {

            
      
      
        get
      
      
        

            {

                
      
      
        string
      
       connStr = ConfigurationManager.ConnectionStrings[
      
        "
      
      
        ConnectionString
      
      
        "
      
      
        ].ConnectionString;

                
      
      
        if
      
       (connection == 
      
        null
      
      
        )

                {

                    connection 
      
      = 
      
        new
      
      
         SqlConnection(connStr);

                    connection.Open();

                }

                
      
      
        else
      
      
        if
      
      (connection.State==
      
        System.Data.ConnectionState.Closed){

                    connection 
      
      = 
      
        new
      
      
         SqlConnection(connStr);

                    connection.Open();

                }
      
      
        else
      
      
        if
      
       (connection.State==
      
        System.Data.ConnectionState.Broken){

                    connection.Close();

                    connection.Open();

                }

                
      
      
        return
      
      
         connection;

            }

        }

        
      
      
        #endregion
      
      
        #region
      
       增,删,改ExecuteNonQuery

        
      
        ///
      
      
        <summary>
      
      
        ///
      
      
         单个数据增,删,改

        
      
      
        ///
      
      
        </summary>
      
      
        ///
      
      
        <param name="sql"></param>
      
      
        ///
      
      
        <returns></returns>
      
      
        public
      
      
        static
      
      
        int
      
       ExecuteNonQuery(
      
        string
      
      
         sql)

        {

            
      
      
        try
      
      
        

            {

                
      
      
        using
      
      (SqlCommand cmd=
      
        new
      
      
         SqlCommand(sql,Conn))

                {

                    
      
      
        int
      
       result =
      
         cmd.ExecuteNonQuery();

                    
      
      
        return
      
      
         result;

                }

            }

            
      
      
        catch
      
      
        (SqlException ex)

            {

                
      
      
        throw
      
      
         ex;

            }

        }

        
      
      
        #endregion
      
      
        #region
      
       带参数的增,删,改ExecuteNonQuery

        
      
        ///
      
      
        <summary>
      
      
        ///
      
      
         带多个参数的增,删,改

        
      
      
        ///
      
      
        </summary>
      
      
        ///
      
      
        <param name="sql"></param>
      
      
        ///
      
      
        <param name="type"></param>
      
      
        ///
      
      
        <param name="values"></param>
      
      
        ///
      
      
        <returns></returns>
      
      
        public
      
      
        static
      
      
        int
      
       ExecuteNonQuery(
      
        string
      
       sql,CommandType type,
      
        params
      
      
         SqlParameter[] values)

        {

            
      
      
        try
      
      
        

            {

                
      
      
        using
      
      (SqlCommand cmd=
      
        new
      
      
         SqlCommand(sql,Conn))

                {

                    cmd.CommandType 
      
      =
      
         type;

                    cmd.Parameters.AddRange(values);
      
      
        //


      
      
        int
      
       result =
      
         cmd.ExecuteNonQuery();

                    
      
      
        return
      
      
         result;

                }

            }

            
      
      
        catch
      
      
        (SqlException ex)

            {

                
      
      
        throw
      
      
         ex;

            }

        }

        
      
      
        #endregion
      
      
        #region
      
       查询语句ExecuteScalar

        
      
        ///
      
      
        <summary>
      
      
        ///
      
      
         查单个值

        
      
      
        ///
      
      
        </summary>
      
      
        ///
      
      
        <param name="sql"></param>
      
      
        ///
      
      
        <returns></returns>
      
      
        public
      
      
        static
      
      
        int
      
       ExecuteScalar(
      
        string
      
      
         sql)

        {

            
      
      
        try
      
      
        

            {

                
      
      
        using
      
      (SqlCommand cmd=
      
        new
      
      
         SqlCommand(sql,Conn))

                {

                    
      
      
        int
      
       result =
      
         Convert.ToInt32(cmd.ExecuteScalar());

                    
      
      
        return
      
      
         result;

                }

            }

            
      
      
        catch
      
      
        (SqlException ex)

            {



                
      
      
        throw
      
      
         ex;

            }

        }

        
      
      
        #endregion
      
      
        #region
      
       带参数的查询语句ExecuteScalar

        
      
        ///
      
      
        <summary>
      
      
        ///
      
      
         带执行类型的ExecuteScalar

        
      
      
        ///
      
      
        </summary>
      
      
        ///
      
      
        <param name="sql"></param>
      
      
        ///
      
      
        <param name="type"></param>
      
      
        ///
      
      
        <param name="values"></param>
      
      
        ///
      
      
        <returns></returns>
      
      
        public
      
      
        static
      
      
        int
      
       ExecuteScalar(
      
        string
      
       sql,CommandType type,
      
        params
      
      
         SqlParameter[] values)

        {

            
      
      
        try
      
      
        

            {

                
      
      
        using
      
      (SqlCommand cmd=
      
        new
      
      
         SqlCommand(sql,Conn))

                {

                    cmd.CommandType 
      
      =
      
         type;

                    cmd.Parameters.AddRange(values);

                    
      
      
        int
      
       result =
      
        Convert.ToInt32(cmd.ExecuteScalar());

                    
      
      
        return
      
      
         result;

                }

            }

            
      
      
        catch
      
      
        (SqlException ex)

            {

                
      
      
        throw
      
      
         ex;

            }

        }

        
      
      
        #endregion
      
      
        #region
      
       查询,返回DataReader

        
      
        ///
      
      
        <summary>
      
      
        ///
      
      
         查询表,获取多个记录

        
      
      
        ///
      
      
        </summary>
      
      
        ///
      
      
        <param name="sql"></param>
      
      
        ///
      
      
        <returns></returns>
      
      
        public
      
      
        static
      
       SqlDataReader ExecuteReader(
      
        string
      
      
         sql)

        {

            
      
      
        try
      
      
        

            {

                
      
      
        using
      
      (SqlCommand cmd=
      
        new
      
      
         SqlCommand(sql,Conn))

                {

                    SqlDataReader dtr 
      
      =
      
         cmd.ExecuteReader();

                    
      
      
        return
      
      
         dtr;

                }

            }

            
      
      
        catch
      
      
        (SqlException ex)

            {

                
      
      
        throw
      
      
         ex;

            }

        }

        
      
      
        #endregion
      
      
        #region
      
       带参数的查询,返回DataReader

        
      
        ///
      
      
        <summary>
      
      
        ///
      
      
         查询表,获取多个记录

        
      
      
        ///
      
      
        </summary>
      
      
        ///
      
      
        <param name="sql"></param>
      
      
        ///
      
      
        <param name="type"></param>
      
      
        ///
      
      
        <param name="values"></param>
      
      
        ///
      
      
        <returns></returns>
      
      
        public
      
      
        static
      
       SqlDataReader ExecuteReader(
      
        string
      
       sql,CommandType type,
      
        params
      
      
         SqlParameter[] values)

        {

            
      
      
        try
      
      
        

            {

                
      
      
        using
      
      (SqlCommand cmd=
      
        new
      
      
         SqlCommand(sql,Conn))

                {

                    cmd.CommandType 
      
      =
      
         type;

                    cmd.Parameters.AddRange(values);

                    SqlDataReader dtr 
      
      =
      
         cmd.ExecuteReader();

                    
      
      
        return
      
      
         dtr;

                }

            }

            
      
      
        catch
      
      
        (SqlException ex)

            {

                
      
      
        throw
      
      
         ex;

            }

        }

        
      
      
        #endregion
      
      
        #region
      
       查询,返回datatable

        
      
        ///
      
      
        <summary>
      
      
        ///
      
      
         返回datatable

        
      
      
        ///
      
      
        </summary>
      
      
        ///
      
      
        <param name="sql"></param>
      
      
        ///
      
      
        <returns></returns>
      
      
        public
      
      
        static
      
       DataTable dataTable(
      
        string
      
      
         sql)

        {

            
      
      
        try
      
      
        

            {

                DataSet dst 
      
      = 
      
        new
      
      
         DataSet();

                SqlCommand cmd 
      
      = 
      
        new
      
      
         SqlCommand(sql,Conn);

                SqlDataAdapter dad 
      
      = 
      
        new
      
      
         SqlDataAdapter(cmd);

                dad.Fill(dst);
      
      
        //
      
      
        在 DataSet 中添加或刷新行
      
      
        return
      
       dst.Tables[
      
        0
      
      
        ];

            }

            
      
      
        catch
      
      
        (SqlException ex)

            {

                
      
      
        throw
      
      
         ex;

            }

        }

        
      
      
        #endregion
      
      
        #region
      
       带参数的查询, 返回dataTable

        
      
        ///
      
      
        <summary>
      
      
        ///
      
      
         返回dataTable

        
      
      
        ///
      
      
        </summary>
      
      
        ///
      
      
        <param name="sql"></param>
      
      
        ///
      
      
        <param name="values"></param>
      
      
        ///
      
      
        <returns></returns>
      
      
        public
      
      
        static
      
       DataTable datatable(
      
        string
      
       sql,
      
        params
      
      
         SqlParameter[] values)

        {

            DataSet dst 
      
      = 
      
        new
      
      
         DataSet();

            SqlCommand cmd 
      
      = 
      
        new
      
      
         SqlCommand(sql,Conn);

            cmd.Parameters.AddRange(values);

            SqlDataAdapter dad 
      
      = 
      
        new
      
      
         SqlDataAdapter(cmd);

            dad.Fill(dst);
      
      
        //
      
      
        在 DataSet 中添加或刷新行
      
      
        return
      
       dst.Tables[
      
        0
      
      
        ];

        }

        
      
      
        #endregion
      
      
        

    }

}
      
    

 

SqlHelper 基类


更多文章、技术交流、商务合作、联系博主

微信扫码或搜索:z360901061

微信扫一扫加我为好友

QQ号联系: 360901061

您的支持是博主写作最大的动力,如果您喜欢我的文章,感觉我的文章对您有帮助,请用微信扫描下面二维码支持博主2元、5元、10元、20元等您想捐的金额吧,狠狠点击下面给点支持吧,站长非常感激您!手机微信长按不能支付解决办法:请将微信支付二维码保存到相册,切换到微信,然后点击微信右上角扫一扫功能,选择支付二维码完成支付。

【本文对您有帮助就好】

您的支持是博主写作最大的动力,如果您喜欢我的文章,感觉我的文章对您有帮助,请用微信扫描上面二维码支持博主2元、5元、10元、自定义金额等您想捐的金额吧,站长会非常 感谢您的哦!!!

发表我的评论
最新评论 总共0条评论