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

SQLHelper帮助类

时间:2016-08-24 00:58:14      阅读:192      评论:0      收藏:0      [点我收藏+]

标签:

通过分装的方法:

 public class SQLHelper
    {
       private static readonly string ConnectionString = ConfigurationManager.AppSettings["conn"].ToString();
       //SqlParameter[]  方便传递数组

       /// <summary>
       /// 主要用于封装Command对象的ExecuteNonQuery方法,用于数据的增删改
       /// </summary>
       /// <param name="conn">Connection对象</param>
       /// <param name="cmdText">Command.CommandText</param>
       /// <param name="cmdType">Command.CommandType</param>
       /// <param name="cmdParams">Command.Parameters</param>
       /// <returns>返回受影响的行数</returns>
       public static int ExecuteNonQuery(string cmdText, CommandType cmdType, params SqlParameter[] cmdParams)
       {
           SqlConnection conn = new SqlConnection(ConnectionString);
           SqlCommand comm = new SqlCommand();

           PrepareCommand(comm, conn, cmdText, cmdType, cmdParams);
           try
           {
               return comm.ExecuteNonQuery();
           }
           catch (SqlException ex)
           {

               throw ex;
           }
           finally {
               conn.Close();
           }
          
       }

       /// <summary>
       /// 封装Command对象的ExecuteReader 方法用于数据的查询
       /// </summary>
       /// <param name="conn">Connection对象</param>
       /// <param name="cmdText">Command.CommandText</param>
       /// <param name="cmdType">Command.CommandType</param>
       /// <param name="cmdParams">Command.Parameters</param>
       /// <returns>返回SqlDataReader对象</returns>
       public static SqlDataReader ExcuteReader(string cmdText, CommandType cmdType, params SqlParameter[] cmdParams)
       {
           SqlConnection conn = new SqlConnection(ConnectionString);
           SqlCommand comm = new SqlCommand();
           PrepareCommand(comm, conn, cmdText, cmdType, cmdParams);
           try
           {
               //自动关闭
               return comm.ExecuteReader(CommandBehavior.CloseConnection);
           }
           catch (SqlException ex)
           {
               
               throw ex;
           }
       }

       /// <summary>
       /// 封装Commond对象的ExecuteScalar方法,用于返回首行首列数据
       /// </summary>
       /// <param name="conn">Connection对象</param>
       /// <param name="cmdText">Command.CommandText</param>
       /// <param name="cmdType">Command.CommandType</param>
       /// <param name="cmdParams">Command.Parameters</param>
       /// <returns>返回的是object单一的值</returns>
       public static object ExecuteScalar(string cmdText, CommandType cmdType, params SqlParameter[] cmdParams)
       {
           SqlConnection conn = new SqlConnection(ConnectionString);
           SqlCommand comm = new SqlCommand();
           PrepareCommand(comm, conn, cmdText, cmdType, cmdParams);
           try
           {
               return comm.ExecuteScalar();
           }
           catch (SqlException ex)
           {

               throw ex;
           }
           finally
           {
               conn.Close();
           }
       }



       /// <summary>
       /// 主要用于返回DataTable 查询的数据
       /// </summary>
       /// <param name="conn">Connection对象</param>
       /// <param name="cmdText">Command.CommandText</param>
       /// <param name="cmdType">Command.CommandType</param>
       /// <param name="cmdParams">Command.Parameters</param>
       /// <returns>返回DataTable对象</returns>
       public static DataTable GetDataTable(string cmdText, CommandType cmdType, params SqlParameter[] cmdParams)
       {
           SqlConnection conn = new SqlConnection();
           SqlCommand comm = new SqlCommand();
           PrepareCommand(comm, conn, cmdText, cmdType, cmdParams);

           SqlDataAdapter da = new SqlDataAdapter();
           da.SelectCommand = comm;
           DataSet ds = new DataSet();
           try
           {
               //自动打开自动关闭  实现断开式的链接
               da.Fill(ds);
               return ds.Tables[0];
           }
           catch (SqlException ex)
           {

               throw ex;
           }
           finally { 
           conn.Close();
           }
         
       }
       /// <summary>
       /// 主要用于给Command对象进行初始化赋值工作
       /// </summary>
       /// <param name="comm">是操作的Comman对象</param>
       /// <param name="conn">Connection对象</param>
       /// <param name="cmdText">Command.CommandText</param>
       /// <param name="cmdType">Command.CommandType</param>
       /// <param name="cmdParams">Command.Parameters</param>
       private static void PrepareCommand(SqlCommand comm, SqlConnection conn, string cmdText, CommandType cmdType, SqlParameter[] cmdParams)
       {
           if (conn.State == ConnectionState.Closed)  conn.Open();
           comm.Connection = conn;
           comm.CommandText = cmdText;
           comm.CommandType = cmdType;
           if (cmdParams != null)
           {
               for (int i = 0; i < cmdParams.Length; i++)
               {
                   comm.Parameters.Add(cmdParams[i]);
               }

           }
       }



    }

 

 

Access数据库的帮助类:

public class AccessHelper
   {
       private static readonly string ConnectionString = ConfigurationManager.AppSettings["conn"].ToString();
       //SqlParameter[]  方便传递数组

       /// <summary>
       /// 主要用于封装Command对象的ExecuteNonQuery方法,用于数据的增删改
       /// </summary>
       /// <param name="conn">Connection对象</param>
       /// <param name="cmdText">Command.CommandText</param>
       /// <param name="cmdType">Command.CommandType</param>
       /// <param name="cmdParams">Command.Parameters</param>
       /// <returns>返回受影响的行数</returns>
       public static int ExecuteNonQuery(string cmdText, CommandType cmdType, params OleDbParameter[] cmdParams)
       {
           OleDbConnection conn = new OleDbConnection(ConnectionString);
           OleDbCommand comm = new OleDbCommand();

           PrepareCommand(comm, conn, cmdText, cmdType, cmdParams);
           try
           {
               return comm.ExecuteNonQuery();
           }
           catch (OleDbException ex)
           {

               throw ex;
           }
           finally
           {
               conn.Close();
           }

       }

       /// <summary>
       /// 封装Command对象的ExecuteReader 方法用于数据的查询
       /// </summary>
       /// <param name="conn">Connection对象</param>
       /// <param name="cmdText">Command.CommandText</param>
       /// <param name="cmdType">Command.CommandType</param>
       /// <param name="cmdParams">Command.Parameters</param>
       /// <returns>返回SqlDataReader对象</returns>
       public static OleDbDataReader ExcuteReader(string cmdText, CommandType cmdType, params OleDbParameter[] cmdParams)
       {
           OleDbConnection conn = new OleDbConnection(ConnectionString);
           OleDbCommand comm = new OleDbCommand();

           PrepareCommand(comm, conn, cmdText, cmdType, cmdParams);
           try
           {
               //自动关闭
               return comm.ExecuteReader(CommandBehavior.CloseConnection);
           }
           catch (OleDbException ex)
           {

               throw ex;
           }
       }

       /// <summary>
       /// 封装Commond对象的ExecuteScalar方法,用于返回首行首列数据
       /// </summary>
       /// <param name="conn">Connection对象</param>
       /// <param name="cmdText">Command.CommandText</param>
       /// <param name="cmdType">Command.CommandType</param>
       /// <param name="cmdParams">Command.Parameters</param>
       /// <returns>返回的是object单一的值</returns>
       public static object ExecuteScalar(string cmdText, CommandType cmdType, params OleDbParameter[] cmdParams)
       {
           OleDbConnection conn = new OleDbConnection(ConnectionString);
           OleDbCommand comm = new OleDbCommand();

           PrepareCommand(comm, conn, cmdText, cmdType, cmdParams);
           try
           {
               return comm.ExecuteScalar();
           }
           catch (OleDbException ex)
           {

               throw ex;
           }
           finally
           {
               conn.Close();
           }
       }



       /// <summary>
       /// 主要用于返回DataTable 查询的数据
       /// </summary>
       /// <param name="conn">Connection对象</param>
       /// <param name="cmdText">Command.CommandText</param>
       /// <param name="cmdType">Command.CommandType</param>
       /// <param name="cmdParams">Command.Parameters</param>
       /// <returns>返回DataTable对象</returns>
       public static DataTable GetDataTable(string cmdText, CommandType cmdType, params OleDbParameter[] cmdParams)
       {
           OleDbConnection conn = new OleDbConnection(ConnectionString);
           OleDbCommand comm = new OleDbCommand();

           PrepareCommand(comm, conn, cmdText, cmdType, cmdParams);

           OleDbDataAdapter da = new OleDbDataAdapter();
           da.SelectCommand = comm;
           DataSet ds = new DataSet();
           try
           {
               //自动打开自动关闭  实现断开式的链接
               da.Fill(ds);
               return ds.Tables[0];
           }
           catch (OleDbException ex)
           {

               throw ex;
           }
           finally
           {
               conn.Close();
           }

       }
       /// <summary>
       /// 主要用于给Command对象进行初始化赋值工作
       /// </summary>
       /// <param name="comm">是操作的Comman对象</param>
       /// <param name="conn">Connection对象</param>
       /// <param name="cmdText">Command.CommandText</param>
       /// <param name="cmdType">Command.CommandType</param>
       /// <param name="cmdParams">Command.Parameters</param>
       private static void PrepareCommand(OleDbCommand comm, OleDbConnection conn, string cmdText, CommandType cmdType, OleDbParameter[] cmdParams)
       {
           if (conn.State == ConnectionState.Closed) conn.Open();
           comm.Connection = conn;
           comm.CommandText = cmdText;
           comm.CommandType = cmdType;
           if (cmdParams != null)
           {
               for (int i = 0; i < cmdParams.Length; i++)
               {
                   comm.Parameters.Add(cmdParams[i]);
               }

           }
       }



   }

 

SQLHelper帮助类

标签:

原文地址:http://www.cnblogs.com/sunliyuan/p/5801314.html

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