Heavens Beyond heavens, there are people outside. Self into the accelerated since, has been considering, the previous reconstruction of the computer room, see others in the D layer added SQL helper, it must be a scholar plus play, and so on, after the next stage of the beef brisket, and saw the people built SQL helper, Do not feel feeling, compared with others, they are my teacher!
Gossip is not much to say, the formal topic: How to write Good SQL Helper? On the macro level, SQL Helper is a fully embodied object-oriented abstraction and encapsulation idea. It extracts the duplicated code and abstracts it for encapsulation, which improves the reuse of the code.
Then it's time to discuss how to write the question. If I first to give you a piece of code, more or less everyone is very inconsistent, and now I draw a concept map, we first look at, it is good to understand:
Everyone looked at the above picture, I do not just one line of the column.
Here's the code:
/* Creator: Qiu Xia * created on: 2014-07-18 * Description: Database helper class * All rights reserved: Qiu Xia * */using system;using system.collections.generic;using System.Text; Using system.data;using system.data.sqlclient;using system.configuration;namespace dal{public class SQLHelper { PRIVATE SqlConnection conn = null; Private SqlCommand cmd = null; Private SqlDataReader SDR = null; Public SQLHelper () {//string connstr = "server=mx;database=newsystem;uid=sa;pwd=123456"; String connstr = configurationmanager.connectionstrings["ConnStr"]. ConnectionString; conn = new SqlConnection (CONNSTR); Private SqlConnection Getconn () {if (conn. state = = connectionstate.closed) {Conn. Open (); } return conn; } #region do not change the parameters of the SQL statement or stored procedure////<summary>/////////////</SUMMARY&G T <param name= "Cmdtext" > adding or deleting SQL statements or stored procedures </param>//<param name= "ct" > Command type </param>//<returns></returns> public int Ex Ecutenonquery (String cmdtext, CommandType ct) {int res; try {SqlCommand cmd = new SqlCommand (Cmdtext, Getconn ()); Cmd.commandtype = ct; res = cmd. ExecuteNonQuery (); } catch (Exception ex) {throw ex; } finally {if (conn. state = = ConnectionState.Open) {Conn. Close (); } conn. Close (); } return res; } #endregion #region Execute parameters or delete or modify SQL statements or stored procedures///<summary>//////// </summary>//<param name= "Cmdtext" > adding or deleting SQL statements or stored procedures </param>//<param name= "ct" > Command type </param>//<returns></returns> public int ExecuteNonQuery (string cmdtext, Sqlparameter[] paras, CommandType ct) {int res; using (cmd = new SqlCommand (Cmdtext, Getconn ())) {cmd.commandtype = ct; Cmd. Parameters.addrange (paras); res = cmd. ExecuteNonQuery (); } return res; #endregion the query SQL statement or stored procedure that #region execute the parameter////<summary>///execute parameter query SQL statement or stored procedure///&L t;/summary>//<param name= "Cmdtext" > adding or deleting SQL statements or stored procedures </param>//<param name= "ct" > Command type &L t;/param>//<returns></returns> public DataTable ExecuteQuery (string cmdtext, CommandType c T) {datatable dt = new DataTable (); cmd = new SqlCommand (Cmdtext, Getconn ()); Cmd.commandtype = ct; using (SDR = cmd. ExecuteReader (commandbehavior.closeconnection)) {dt. Load (SDR); } return DT; #endregion #region Execute a query SQL statement or stored procedure with parameters////<summary>//Execute query SQL statement or stored procedure with parameters/// </summary>//<param name= "Cmdtext" > Querying SQL statements or stored procedures </param>//<param Name= "Paras" > Parameters Collection </param>//<param name= "ct" > Command type </param>//<returns></returns> PU Blic DataTable ExecuteQuery (String Cmdtext, Sqlparameter[] paras, CommandType ct) {datatable dt = new D Atatable (); cmd = new SqlCommand (Cmdtext, Getconn ()); Cmd.commandtype = ct; Cmd. Parameters.addrange (paras); using (SDR = cmd. ExecuteReader (commandbehavior.closeconnection)) {dt. Load (SDR); } return DT; } #endregion}}
If you understand, the code is not difficult to understand, in the reconstruction of the room, there are many SqlClient function is not clear, so, embodies a little: not afraid not to know, afraid do not know.