C#的通用DbHelper类(支持数据连接池)

简介: C#的通用DbHelper类

每次新项目的时候,都要从头去找一遍数据库工具类。这里分享一个简单实用的C#的通用DbHelper工具类,支持数据连接池。

连接池配置

<connectionStrings>
  <add name="dh_web" connectionString="Data Source=xxx.com;Initial Catalog=xx_db;User ID=xx;Password=**;
      pooling=true;max pool size=200" providerName="System.Data.SqlClient"/>
</connectionStrings>

DbHelper类

public class DBHelper
    {
        private static string connectionString = ConfigurationManager.ConnectionStrings["dh_web"].ConnectionString;
        //不带参数的执行命令
        public static int ExecuteCommand(string safeSql)
        {
            using (SqlConnection connection = new SqlConnection(connectionString))
            {
                connection.Open();
                SqlCommand cmd = new SqlCommand(safeSql, connection);
                return cmd.ExecuteNonQuery();
            }
        }
        //带参数的执行命令
        public static int ExecuteCommand(string sql, params SqlParameter[] values)
        {
            using (SqlConnection connection = new SqlConnection(connectionString))
            {
                connection.Open();
                SqlCommand cmd = new SqlCommand(sql, connection);
                cmd.Parameters.AddRange(values);
                return cmd.ExecuteNonQuery();
            }
        }
        public static int GetScalar(string safeSql)
        {
            using (SqlConnection connection = new SqlConnection(connectionString))
            {
                connection.Open();
                SqlCommand cmd = new SqlCommand(safeSql, connection);
                return Convert.ToInt32(cmd.ExecuteScalar());
            }
        }
        public static int GetScalar(string sql, params SqlParameter[] values)
        {
            using (SqlConnection connection = new SqlConnection(connectionString))
            {
                connection.Open();
                SqlCommand cmd = new SqlCommand(sql, connection);
                cmd.Parameters.AddRange(values);
                return Convert.ToInt32(cmd.ExecuteScalar());
            }
        }
        public static SqlDataReader GetReader(string safeSql)
        {
            SqlConnection connection = new SqlConnection(connectionString);
            connection.Open();
            SqlCommand cmd = new SqlCommand(safeSql, connection);
            return cmd.ExecuteReader(CommandBehavior.CloseConnection);
        }
        public static SqlDataReader GetReader(string sql, params SqlParameter[] values)
        {
            SqlConnection connection = new SqlConnection(connectionString);
            connection.Open();
            SqlCommand cmd = new SqlCommand(sql, connection);
            cmd.Parameters.AddRange(values);
            return cmd.ExecuteReader(CommandBehavior.CloseConnection);
        }
        public static DataTable GetDataSet(string safeSql)
        {
            using (SqlConnection connection = new SqlConnection(connectionString))
            {
                DataSet ds = new DataSet();
                SqlCommand cmd = new SqlCommand(safeSql, connection);
                SqlDataAdapter da = new SqlDataAdapter(cmd);
                da.Fill(ds);
                return ds.Tables[0];
            }
        }
        public static DataTable GetDataSet(string sql, params SqlParameter[] values)
        {
            using (SqlConnection connection = new SqlConnection(connectionString))
            {               
                DataSet ds = new DataSet();
                SqlCommand cmd = new SqlCommand(sql, connection);
                cmd.Parameters.AddRange(values);
                SqlDataAdapter da = new SqlDataAdapter(cmd);
                da.Fill(ds);
                return ds.Tables[0];
            }
        }
    }

注意:

CommandBehavior.CloseConnection解决了流读取数据模式下,数据库连接不能有效关闭的情况.

当某个XXXDataReader对象在生成时使用了CommandBehavior.CloseConnection,那数据库连接将在XXXDataReader对象关闭时自动关闭.


相关文章
|
5天前
|
C# 数据库
c# dev Form1 gridview1使用Form2 gridview1的数据
c# dev Form1 gridview1使用Form2 gridview1的数据
|
5天前
|
C#
C#学习相关系列之数据类型类的三大特性(二)
C#学习相关系列之数据类型类的三大特性(二)
|
3天前
|
C#
C#的类和对象的概念学习案例刨析
【5月更文挑战第17天】C#是一种面向对象的语言,以类和对象为核心。类作为对象的模板,定义了属性(如Name, Age)和行为(如Greet)。对象是类的实例,可设置属性值。封装通过访问修饰符隐藏实现细节,如Customer类的私有name字段通过Name属性访问。继承允许新类(如Employee)从现有类(Person)继承并扩展。多态让不同对象(如Circle, Square)共享相同接口(Shape),实现抽象方法Area,提供灵活的代码设计。
9 1
|
5天前
|
C#
c# 所有类的最终基类:Object
c# 所有类的最终基类:Object
7 0
|
5天前
|
XML 存储 开发框架
c#教你网站数据轻松解析抓取,HtmlAgilityPack解析的奇妙之处
c#教你网站数据轻松解析抓取,HtmlAgilityPack解析的奇妙之处
13 0
|
5天前
|
安全 C#
C#Webrequest类的使用
C#Webrequest类的使用
10 0
|
5天前
|
SQL 存储 C#
C# Web控件与数据感应之 TreeView 类
C# Web控件与数据感应之 TreeView 类
|
5天前
|
SQL 存储 Oracle
C# Web控件与数据感应之 CheckBoxList 类
C# Web控件与数据感应之 CheckBoxList 类
|
5天前
|
SQL 存储 Oracle
C# Web控件与数据感应之 Control 类
C# Web控件与数据感应之 Control 类
|
5天前
|
SQL 存储 Oracle
C# Web控件与数据感应之 ListControl 类
C# Web控件与数据感应之 ListControl 类