Every time you have a new project, you have to search for the database tool class from the beginning. Here we share a simple and practical C# general DbHelper tool class that supports data connection pooling.
Connection pool configuration
<connectionStrings> <add name="dh_web" connectionString="Data Source=;Initial Catalog=xx_db;User ID=xx;Password=**; pooling=true;max pool size=200" providerName=""/> </connectionStrings>
DbHelper class
public class DBHelper { private static string connectionString = ["dh_web"].ConnectionString; //Execute command without parameters public static int ExecuteCommand(string safeSql) { using (SqlConnection connection = new SqlConnection(connectionString)) { (); SqlCommand cmd = new SqlCommand(safeSql, connection); return (); } } //Execute command with parameters public static int ExecuteCommand(string sql, params SqlParameter[] values) SqlCommand cmd = new SqlCommand(sql, connection); (values); public static int GetScalar(string safeSql) return Convert.ToInt32(()); public static int GetScalar(string sql, params SqlParameter[] values) public static SqlDataReader GetReader(string safeSql) SqlConnection connection = new SqlConnection(connectionString); (); SqlCommand cmd = new SqlCommand(safeSql, connection); return (); public static SqlDataReader GetReader(string sql, params SqlParameter[] values) SqlCommand cmd = new SqlCommand(sql, connection); (values); public static DataTable GetDataSet(string safeSql) DataSet ds = new DataSet(); SqlDataAdapter da = new SqlDataAdapter(cmd); (ds); return [0]; public static DataTable GetDataSet(string sql, params SqlParameter[] values) { }
Notice:
Solved the situation where the database connection cannot be effectively closed in stream reading data mode.
When an XXXDataReader object is used during generation, the database connection will be automatically closed when the XXXDataReader object is closed.
This is the end of this article about C#'s general DbHelper class (supports data connection pooling). For more relevant C# general DbHelper class content, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!