.net DBHelper

  DBHelper.cs是程序来连接数据的,也是一个程序必不可少的一个类(本人道行浅,目前这样认为)。由于一个程序基本写一次,容易忘记。所有写在这里备注

 

 

首先是引用

 

using System.Data;
using System.Data.SqlClient;
using System.Configuration;

 

然后代码:

conn.Close();
conn.Dispose();
cmd.Dispose();
这几个为了防患于未然也加上去了

  public class DBHelper
    {
        private static readonly string strconnection = ConfigurationManager.ConnectionStrings["Conn"].ConnectionString;
        private static SqlConnection conn = new SqlConnection(strconnection);
        //创建给sqlreadr使用
        public static SqlConnection GetConnection() { return conn; }
        /// <summary>
        /// 打开数据库链接
        /// </summary>
        public static void OpenSqlConnection()
        {
            if (GetConnection().State == ConnectionState.Closed) { DBHelper.GetConnection().Open(); }
        }
        /// <summary>
        /// 关闭数据库链接
        /// </summary>
        public static void ClosSqlConnection()
        {
            if (GetConnection().State == ConnectionState.Open) { DBHelper.GetConnection().Close(); }
        }


        /// <summary>
        /// 基础ExecuteNonQuery 方法
        /// </summary>
        /// <param name="SQLString">执行的sql语句</param>
        /// <param name="type">是否执行存储过程·</param>
        /// <param name="prep">存储过程参数</param>
        /// <returns></returns>
        private static int ExecuteNonQuery(string SQLString, CommandType type, SqlParameter[] prep)
        {
            using (SqlConnection conn = new SqlConnection(strconnection))
            {
                using (SqlCommand cmd = new SqlCommand(SQLString, conn))
                {
                    int r = 0;
                    try
                    {
                        conn.Open();
                        cmd.CommandType = type;
                        if (prep != null && prep.Length > 0)
                        {
                            foreach (SqlParameter p in prep)
                            {
                                cmd.Parameters.Add(p);
                            }
                        }
                        r = cmd.ExecuteNonQuery();
                    }
                    catch (System.Data.SqlClient.SqlException ex) { throw new Exception(ex.Message); }
                    finally
                    {
                        conn.Close();
                        conn.Dispose();
                        cmd.Dispose();
                    }
                    return r;
                }
            }

        }

        /// <summary>
        /// 基础datatable
        /// </summary>
        /// <param name="SQLString">执行的sql语句</param>
        /// <param name="type">是否执行存储过程·</param>
        /// <param name="prep">存储过程参数</param>
        /// <returns></returns>
        private static DataTable GetDataTable( string SQLString, CommandType type, SqlParameter[] prep)
        {
            using (SqlConnection conn = new SqlConnection(strconnection))
            {
                using (SqlCommand cmd = new SqlCommand(SQLString, conn))
                {
                    SqlDataAdapter dap = null;
                    DataTable dt = null;
                    try
                    {
                        cmd.CommandType = type;
                        if (prep != null && prep.Length > 0)
                        {
                            foreach (SqlParameter p in prep)
                            {
                                cmd.Parameters.Add(p);
                            }
                        }
                        dap = new SqlDataAdapter(cmd);
                        dt = new DataTable();
                        dap.Fill(dt);
                    }
                    catch (Exception ex) { }
                    finally
                    {
                        conn.Close();
                        conn.Dispose();
                        cmd.Dispose();
                    }
                    return dt;
                }
            }
        }

        /// <summary>
        /// 基础SqlDataReader
        /// </summary>
        /// <param name="SQLString">执行的sql语句</param>
        /// <param name="type">是否执行存储过程·</param>
        /// <param name="prep">存储过程参数</param>
        /// <returns></returns>
        private static SqlDataReader GetList(string SQLString, CommandType type, SqlParameter[] prep)
        {
            SqlCommand cmd = null;
            SqlDataReader dr = null;
            cmd = new SqlCommand(SQLString, conn);
            if (prep != null && prep.Length > 0)
            {
                cmd.Parameters.AddRange(prep);
            }
            cmd.CommandType = type;
            try { dr = cmd.ExecuteReader(); }
            catch (Exception ex) { }
            return dr;
        }


        //调用ExecuteNonQuery 方法
        public static int ExecuteNonQuery(string SQLString, SqlParameter[] prep) {
            return ExecuteNonQuery(SQLString, CommandType.StoredProcedure, prep);
        }
        public static int ExecuteNonQuery(string SQLString) {
            return ExecuteNonQuery(SQLString, CommandType.Text, null);
        }
        //调用GetDataTable 方法
        public static DataTable GetDataTable(string SQLString, SqlParameter[] prep) {
            return GetDataTable(SQLString, CommandType.StoredProcedure, prep);
        }
        public static DataTable GetDataTable(string SQLString)
        {
            return GetDataTable(SQLString, CommandType.Text, null);
        }
        //调用SqlDataReader  调用此方法需要先打开conn  使用完毕之后关闭conn
        public static SqlDataReader GetList(string SQLString, SqlParameter[] prep)
        {
            return GetList(SQLString, CommandType.StoredProcedure, prep);
        }
        public static SqlDataReader GetList(string SQLString)
        {
            return GetList(SQLString, CommandType.Text, null);
        }


    }

 

转载于:https://www.cnblogs.com/1439107348s/p/8017025.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
个人曾经写过的数据访问接口,包含:MSSQL、Mysql、Oracle等数据库的公共处理接口。可以拿过来直接使用,放在手里好多年了。 IDatabase接口声明如下: namespace Simple.Database { /// /// IDatabase 接口 /// public interface IDatabase { DbConnection dbConn { get; set; } /// /// 创建 DbConnection 对象实例。 /// /// DbConnection 对象实例。 DbConnection CreateConnection(); /// /// 创建 DbCommand 对象实例。 /// /// DbCommand 对象实例。 DbCommand CreateCommand(); /// /// 创建 DbCommand 对象实例。 /// /// Sql 语句或存储过程名。 /// CommandType 参数。 /// DbCommand 对象实例。 DbCommand CreateCommand(string text, CommandType type); /// /// 创建 DbCommand 对象实例。 /// /// Sql 语句或存储过程名。 /// CommandType 参数。 /// 参数集合。 /// DbCommand 对象实例。 DbCommand CreateCommand(string text, CommandType type, IDataParameter[] paras); /// /// 创建 DbCommand 对象实例。 /// /// DbConnection 对象。 /// Sql 语句或存储过程名。 /// CommandType 参数。 /// 参数集合。 /// DbCommand 对象实例。 DbCommand CreateCommand(DbConnection conn, string text, CommandType type, IDataParameter[] paras); /// /// 创建 DbDataAdapter 对象实例。 /// /// DbDataAdapter 对象实例。 DbDataAdapter CreateDataAdapter(); /// /// 创建 DbParameter 对象实例。 /// /// DbParameter 对象实例。 DbParameter CreateParameter(); /// /// 创建 DbParameter 对象实例。 /// /// 参数名称。 /// 参数值。 /// DbParameter 对象实例。 DbParameter CreateParameter(string name, Object value); /// /// 创建 DbParameter 对象实例。 /// /// 参数名称。 /// 参数类型。 /// DbParameter 对象实例。 DbParameter CreateParameter(string name, DbType type); /// /// 创建 DbParameter 对象实例。 /// /// 参数名称。 /// 参数类型。 /// 数据的最大大小。 /// DbParameter 对象实例。 DbParameter CreateParameter(string name, DbType type, int size); /// /// 获取指定长度数据的 DataSet 对象。 /// /// 要读取的 Sql 语句。 /// 开始读取位置的索引。 /// 待读取记录集的长度。 /// DataSet 对象。 DataSet GetDataSet(string sql, int start, int length); /// /// 获取指定长度数据的 DataTable 对象。 /// /// 要读取的 Sql 语句。 /// 开始读取位置的索引。 /// 待读取记录集的长度。 /// DataTable 对象。 DataTable GetDataTable(string sql, int start, int length); /// /// 执行Insert、Update、Delete等操作,并返回受影响的记录数。 /// /// 要执行的 Sql 语句。 /// 受影响的记录数。 int GetEffect(string sql); /// /// 执行 Insert、Update、Delete 等操作,并返回受影响的记录数。 /// /// 要执行的 Sql 语句或存储过程名等。 /// CommandType 的类型,即该命令是 Sql 语句,还是存储过程名等。 /// 受影响的记录数。 int GetEffect(string sql, CommandType type); /// /// 执行带参数的 Sql 语句或存储过程,并返回受影响的记录数。 /// /// 要执行的 Sql 语句或存储过程名等。 /// CommandType 参数类型,即该命令是 sql 语句,还是存储过程名等。 /// 参数集合。 /// 受影响的记录数。 int GetEffect(string text, CommandType type, IDataParameter[] paras); /// /// /// /// /// /// /// /// /// int GetEffect(DbConnection conn, string text, CommandType type, IDataParameter[] paras, DbTransaction DbTrans); /// /// /// /// /// List ExecuteTransaction(params string[] sqls); /// /// 执行 Select 语句,并返回 DataSet 对象。 /// /// 要执行的 Sql 语句。 /// DataSet 对象。 DataSet GetDataSet(string sql); /// /// 执行 Select 语句或存储过程,并返回 DataSet 对象。 /// /// 要执行的 Sql 语句或存储过程名等。 /// CommandType 参数类型,即该命令是 sql 语句,还是存储过程名等。 /// DataSet 对象。 DataSet GetDataSet(string text, CommandType type); /// /// 执行带参数的 Sql 语句或存储过程,并返回 DataSet 对象。 /// /// 要执行的 Sql 语句或存储过程名等。 /// CommandType 参数类型,即该命令是 sql 语句,还是存储过程名等。 /// 参数集合。 /// DataSet 对象。 DataSet GetDataSet(string text, CommandType type, IDataParameter[] paras); /// /// 执行 Select 语句,并返回 DataTable 对象。 /// /// 要执行的 Sql 语句。 /// DataTable 对象。 DataTable GetDataTable(string sql); /// /// 执行 Select 语句或存储过程,并返回 DataTable 对象。 /// /// 要执行的 Sql 语句或存储过程名等。 /// CommandType 参数类型,即该命令是 sql 语句,还是存储过程名等。 /// DataTable 对象。 DataTable GetDataTable(string text, CommandType type); /// /// 执行带参数的 Sql 语句或存储过程,并返回 DataTable 对象。 /// /// 要执行的 Sql 语句或存储过程名等。 /// CommandType 参数类型,即该命令是 sql 语句,还是存储过程名等。 /// 参数集合。 /// DataTable 对象。 DataTable GetDataTable(string text, CommandType type, IDataParameter[] paras); /// /// 获取查询所返回的结果集中第一行第一列的值。 /// /// 要处理的 sql 语句(包含待查询的字段)。 /// 字段值。 object GetField(string sql); /// /// 获取查询所返回的结果集中第一行指定列的值。 /// /// 待查询的数据表名称。 /// 待获取字段的列名。 /// 字段值。 object GetField(string sql, string field); /// /// 获取查询所返回的结果集中第一行指定列集合的值。 /// /// 要处理的 sql 语句。 /// 待获取字段的列表。 /// 字段值集合。 object[] GetField(string sql, params string[] fields); } }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值