SQLHelper 数据库访问助手类

SQLHelper类执行对数据库的访问,并返回值:连接DB的方法以及SqlCommand的各种方法的重写(传入参数,参数类型,返回的值)等。

App_Code文件夹存放网站中通用的类,App_Data文件夹存放一些数据文件XML文件等,WebConfig文件中存放全局的参数。

缓存可以提高对DB的访问效率,.CS文件更改后需要编译重新生成。

try,catch中可以把错误日志写到Log中。

 

using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient; // 访问数据库



///   <summary>
///  
///   </summary>
public  class SQLHelper
{
    SqlConnection sqlCon;
    SqlCommand sqlCom;
    SqlDataReader sdr;
     public SQLHelper()
    {
     
    }
     // 获取一个打开的连接
     private SqlConnection getCon() // 调用时会得到一个已经 打开的sqlConnection
    {
         string strCon = ConfigurationManager.ConnectionStrings[ " conStr "].ConnectionString; // 获得web.config中的连接字符串
         if (sqlCon== null) // 还没有建立连接
        {
            sqlCon =  new SqlConnection(strCon);
            sqlCon.Open();
        }
         if (sqlCon.State==ConnectionState.Closed)
        {
            sqlCon.Open(); // 如果处于关闭状态,不需要建立连接,只需要将其打开

        }
         return sqlCon;
    }

     // 执行无参数的ExecuteNonQuery

     public  int ExecuteNonQuery( string sql,CommandType ct)
    {
         int res =  0;
         try
        {
            sqlCom =  new SqlCommand(sql, getCon());
            sqlCom.CommandType = ct; // sql语句类型
            res = sqlCom.ExecuteNonQuery();
        }
         catch (Exception ex)
        {
             throw ex; // 异常处理机制,可以把出错信息写入log中
        }
         finally
        {
             // 关闭连接
             if (sqlCon.State==ConnectionState.Open)
            {
                sqlCon.Close();
            }
        }
         return res;
 
    }

     // 执行的参数的NonQuery

     public  int ExecuteNonQuery( string sql,SqlParameter[] sqlPara, CommandType ct)
    {
         int res =  0;
         try
        {
            sqlCom =  new SqlCommand(sql, getCon());
            sqlCom.CommandType = ct; // sql语句类型
            sqlCom.Parameters.AddRange(sqlPara); // 添加参数数组
            res = sqlCom.ExecuteNonQuery();
            sqlCom.Parameters.Clear(); // 清理参数,清空
        }
         catch (Exception ex)
        {
             throw ex; // 异常处理机制,可以把出错信息写入log中
        }
         finally
        {
             // 关闭连接
             if (sqlCon.State == ConnectionState.Open)
            {
                sqlCon.Close();
            }
        }
         return res;

    }

     // 执行没有参数的ExcuteScalar

     public  object ExecuteScalar( string sql, CommandType ct)
    {
         object res =  null;
         try
        {
            sqlCom =  new SqlCommand(sql, getCon());
            sqlCom.CommandType = ct;
            res = sqlCom.ExecuteScalar();

        }
         catch (Exception ex)
        {

             throw ex;
        }
         finally
        {
             if (sqlCon.State == ConnectionState.Open)
            {
                sqlCon.Close();
            }

        }
         return res;
    }

     //  ExcuteScalar方法,返回object类型

     public  object ExecuteScalar( string sql, SqlParameter[] sqlPara, CommandType ct)
    {
         object res= null;
         try
        {
            sqlCom =  new SqlCommand(sql,getCon());
            sqlCom.CommandType = ct;
            sqlCom.Parameters.AddRange(sqlPara);
            res = sqlCom.ExecuteScalar();
            sqlCom.Parameters.Clear();
        }
         catch (Exception ex)
        {
             throw ex;
        }
         finally
        {
             if (sqlCon.State == ConnectionState.Open)
            {
                sqlCon.Close();
            }
        }
         return res;
    }

 

     // 执行无参数的ExecuteReader

     public SqlDataReader ExecuteReader( string sql,CommandType ct)
    {
        SqlDataReader sdr =  null;
         try
        {
            sqlCom =  new SqlCommand(sql,getCon());
            sqlCom.CommandType = ct;  
             using (sdr = sqlCom.ExecuteReader(CommandBehavior.CloseConnection)) // sqlDataReader注销释放空间时,同时关闭数据库的连接
            {
                
            }
        }
         catch (Exception ex)
        {

             throw ex;
        }
         finally
        {
             if (sqlCon.State == ConnectionState.Open)
            {
                sqlCon.Close();
            }
        }
         return sdr;
 
    }
     //  执行有参数的ExecuteReader

     public SqlDataReader ExecuteReader( string sql,SqlParameter[] sqlPara,CommandType ct)
    {
        SqlDataReader sdr =  null;
        sqlCom =  new SqlCommand(sql,getCon());
        sqlCom.Parameters.AddRange(sqlPara);
         using (sdr=sqlCom.ExecuteReader(CommandBehavior.CloseConnection))
        {
            
        }
         return sdr;
    }

     //  执行无参数的ExecuteQuery
     public DataTable ExecuteQuery( string sql,CommandType ct)
    {
        DataTable dt =  null;
        SqlDataReader sdr =  null;
         try
        {
            sqlCom =  new SqlCommand(sql, getCon());
            sqlCom.CommandType = ct;
             using (sdr = sqlCom.ExecuteReader(CommandBehavior.CloseConnection))
            {
                dt.Load(sdr);
            }

        }
         catch (Exception ex)
        {

             throw ex;
        }
         finally
        {
             if (sqlCon.State==ConnectionState.Open)
            {
                sqlCon.Close();
            }
        }
         return dt;
    }

     //  执行有参数的ExecuteQuery
     public DataTable ExecuteQuery( string sql, SqlParameter[] sqlPara, CommandType ct)
    {
        DataTable dt =  null;
        SqlDataReader sdr =  null;
        sqlCom =  new SqlCommand(sql,getCon());
        sqlCom.CommandType = ct;
        sqlCom.Parameters.AddRange(sqlPara);
         using (sdr=sqlCom.ExecuteReader(CommandBehavior.CloseConnection))
        {
            dt.Load(sdr);
        }
         return dt;
    }
}

 

<connectionStrings>
<add name= " conStr " connectionString= " server=.;database=schoolnews;uid=sa;pwd=; "/>
</connectionStrings>

 

 

 

 

转载于:https://www.cnblogs.com/hishanghai/archive/2012/05/27/2520647.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值