Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to encapsulate DBHelper classes in C #

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)05/31 Report--

In this article, the editor introduces in detail "how to encapsulate DBHelper classes in C#". The content is detailed, the steps are clear, and the details are handled properly. I hope this article "how to encapsulate DBHelper classes in C#" can help you solve your doubts.

DBHelper literally means "database help class". Because the persistence layer needs to interact with the database, each interaction will repeatedly load the driver, fill in the database connection information, establish (obtain) the database connection, and close the database, resulting in a large amount of redundant code in the code, so extracting the repeated code that needs to be executed becomes a DBHelper. A simple DBHelper provides the most basic database initialization connection and driver, and provides the API to connect to the database and the API to close the database. The usual practice is to make these API static and call directly through the class. API, so it greatly simplifies the writing of the code, improves the maintainability of the code, and is conducive to changing the database and driver.

Detailed code: using System;using System.Collections.Generic;using System.Data;using System.Data.SqlClient;using System.Linq;using System.Text;using System.Threading.Tasks;namespace WindowsFormsApplication1 {class DBHelper {/ / SQL connection string-SQL authentication login public static string connStr = "server=.;database=DBTEST;uid=sa;pwd=123456;" / / SQL connection string-login / / public static string connStr = "Integrated Security=SSPI;Persist Security Info=False;Initial Catalog=DBTEST;Data Source=." by Windows authentication; / / read configuration file appSettings node read string (need to add reference System.Configuration) / / public static string connStr = ConfigurationManager.AppSettings ["DefaultConn"] .ToString () / / the corresponding configuration file is as follows: / / read the configuration file ConnectionStrings node read string (need to add reference System.Configuration) / / public static string connStr = ConfigurationManager.ConnectionStrings ["DefaultConn"] .ConnectionString / / the corresponding configuration file is as follows: / / public static SqlConnection conn = null; public static SqlDataAdapter adp = null # region connect to database / connect to database / public static void OpenConn () {if (conn = = null) {conn = new SqlConnection (connStr); conn.Open () } if (conn.State = = System.Data.ConnectionState.Closed) {conn.Open ();} if (conn.State = = System.Data.ConnectionState.Broken) {conn.Close (); conn.Open () } # endregion # region prepare / prepare to execute a SQL statement / SQL statement to be executed public static void PrepareSql (string sql) {OpenConn () before executing the SQL statement / / Open database connection adp = new SqlDataAdapter (sql, conn) } # endregion # region set and get the parameter of the sql statement / parameter name / parameter value public static void SetParameter (string parameterName, object parameterValue) {parameterName = "@" + parameterName.Trim () If (parameterValue = = null) parameterValue = DBNull.Value; adp.SelectCommand.Parameters.Add (new SqlParameter (parameterName, parameterValue)) } # endregion # region execute SQL statement / execute non-query SQL statement / number of affected rows public static int ExecNonQuery () {int result = adp.SelectCommand.ExecuteNonQuery (); conn.Close (); return result } / execute query SQL statement / DataTable type query result public static DataTable ExecQuery () {DataTable dt = new DataTable (); adp.Fill (dt); conn.Close (); return dt } / execute query SQL statement / query result of SqlDataReader type. SqlDataReader needs to manually close public static SqlDataReader ExecDataReader () {return adp.SelectCommand.ExecuteReader (CommandBehavior.CloseConnection). } / execute the query SQL statement / the first column of the first row of the query result public static object ExecScalar () {object obj = adp.SelectCommand.ExecuteScalar (); conn.Close (); return obj } # endregion}} here, this article "how to encapsulate DBHelper classes in C#" has been introduced. If you want to master the knowledge points of this article, you still need to practice and use it yourself. If you want to know more about related articles, welcome to follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report