SQLHelper帮助类

通过分装的方法:

 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]);
               }

           }
       }

   }
时间: 2024-08-07 02:56:01

SQLHelper帮助类的相关文章

微软SQLHelper.cs类 中文版

using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Configuration; using System.Data; using System.Data.SqlClient; using System.Xml; using System.Collections; namespace LiuYanBanT { public class SqlHelper

自用C#后端SqlHelper.cs类

自用SqlHelper.cs类,此类来自软谋教育徐老师课程SqlHelper.cs! 1 using System; 2 using System.Collections; 3 using System.Collections.Generic; 4 using System.Configuration; 5 using System.Data; 6 using System.Data.SqlClient; 7 using System.Linq; 8 using System.Web; 9 10

C#版SQLHelper.cs类

using System; using System.Data; using System.Xml; using System.Data.SqlClient; using System.Collections; using System.Configuration; namespace BookDAL { /// <summary> /// SqlServer数据访问帮助类 /// </summary> public sealed class SqlHelper { #region

万能的SQLHelper帮助类

/// <summary> /// 数据库帮助类 /// </summary> public class SQLHelper { private static string connStr = "server=.;database=***;uid=**;pwd=***"; //定义对象 private static SqlConnection conn = null; private static SqlCommand cmd = null; private s

微软C#版SQLHelper.cs类

转载自:http://blog.csdn.net/fengqingtao2008/article/details/17399247 1 using System; 2 using System.Data; 3 using System.Xml; 4 using System.Data.SqlClient; 5 using System.Collections; 6 using System.Configuration; 7 8 namespace BookDAL 9 { 10 /// <summ

SqlHelper,JSonHelper类

1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Web; 5 using System.Data.SqlClient; 6 using System.Data; 7 using System.Configuration; 8 9 public class SqlHelper 10 { 11 public static readonly string conString

SQLHELPER 帮助类

using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks; namespace 数据库的读取{ using System.Data; using System.Data.SqlClient; class SQLHelper { static string sqlconn = System.Configuration.Configura

【ASP.NET-中级】SQLHelper数据访问公共类

ASP.NET开发中的三层开发思想指的是UI层(界面显示层),BLL层(业务逻辑层),DAL层(数据访问层)三层,三层之间通过函数的调用来达到降低耦合,易于系统维护的目的,SQLHelper助手类的主要作用在于接收并执行来自各个数据表累传来的sql语句或存储过程.一般的SQLHelper类中主要包括以下几个函数功能: 1.执行不带参数的增删改方法 2.执行带参数的增删改方法. 3.执行不带参数的查询方法. 4.执行带参数的查询方法. 作为一个程序员SQLHelper类编写的好坏不仅影响着系统的可

C# SqlHelper类的数据库操作

1 #region 私有构造函数和方法 2 3 private SqlHelper() { } 4 5 /// <summary> 6 /// 将SqlParameter参数数组(参数值)分配给SqlCommand命令. 7 /// 这个方法将给任何一个参数分配DBNull.Value; 8 /// 该操作将阻止默认值的使用. 9 /// </summary> 10 /// <param name="command">命令名</param>