After accepting the three-story idea, SqlHelper is being preached about the number of advantages it brings to our code, the amount of time and energy that the coder spends, and so forth the words of praise. Self is also doubtful, after all, I have no personal experience, so there is no great experience. And now, many times I used the SqlHelper, shuttle between the layers really also realized that it brings us the simplicity, combined with a lot of people's version of their own to write their own version, the following is the specific code:
<span style= "FONT-FAMILY:KAITI_GB2312;FONT-SIZE:18PX;" >imports system.dataimports System.Data.SqlClientImports system.configuration ' Add in Manager to use ' ' <summary> ' SqlHelper class ' </summary> ' <remarks ></remarks >public class Sqlhelperdal ' Get the number According to the library's connection string private ReadOnly strconnection as String = "Server=zhanghui-pc;database=charge-sys;user id=sa;password=1234 56 "' Set connection Dim conn As SqlConnection = new SqlConnection (strconnection) ' Define cmd command Dim cmd As New SqlCommand ' <summary> ' perform add, delete, change three operations (with reference), ' </summary> ' ' <param name= ' cm Dtext "> </param >" "<param name=" Cmdtype "> </param >" <param name= "para S "> </param >" <returns></returns> public Function execadddelupdate (ByVal Cmdtext as String, ByVal Cmdtype as CommandType, ByVal paras as SqlParameter ()) as BooleaN ' assigns the passed-in value to cmd cmd, respectively. Parameters.addrange (paras) cmd. CommandType = Cmdtype cmd. Connection = conn cmd. CommandText = Cmdtext Try Conn. Open () Return cmd. ExecuteNonQuery () cmd. Parameters.clear () Catch ex as Exception Return 0 Finally call Closecmd (CMD) Call CLOSECONN (conn) End Try End Function ' <summary> ' perform add, delete, change three operations (no reference) ' </summary> "' <param name=" Cmdtext "> </param >" "<param name=" Cmdtype "> </param > ' < Returns>intergers </returns > ' <remarks > </remarks > Public Function Execadddelupdateno (ByVal cmdtext as String, ByVal Cmdtype as CommandType) as Integer ' is the cmd command to execute. CommandText = Cmdtext cmd. CommandType = Cmdtype cmd. Connection = Conn ' Executes the action Try Conn. Open () Return CMd. ExecuteNonQuery () cmd. Parameters.clear () Catch ex as Exception Return 0 Finally call Closecmd (CMD) Call CLOSECONN (conn) End Try end Function ' <summary> ' perform query operation (with reference) ' </summary> "' <param name=" Cmdtext "> </param>" "<param name=" Cmdtype "> </param > ' <param name= "Paras" > </param > ' <returns></returns > ' <remarks></remarks > Public Fu Nction Execselect (ByVal cmdtext as String, ByVal Cmdtype as CommandType, ByVal paras as SqlParameter ()) as DataTable Dim sqladapter As SqlDataAdapter Dim dt As New DataTable Dim ds As New DataSet ' to cmd assignment cmd.c Ommandtext = Cmdtext cmd. CommandType = Cmdtype cmd. Connection = conn cmd. Parameters.addrange (paras) sqladapter = New SqlDataAdapter (cmd) instantiation of adapter Object Try Sqladapter.fill (DS) dt = ds. Tables (0) cmd. Parameters.clear () Catch ex as Exception MsgBox ("Query Failed", CType (vbOKOnly + msgboxstyle.exclamation, MsgBox Style), "warning") Finally call Closecmd (cmd) End Try Return dt End Function ' <summ Ary> ' operation to perform the query (no parameter) ' </summary> ' <param name= ' cmdtext ' ></param> ' <param name = "Cmdtype" ></param> "<returns></returns>" <remarks></remarks> public funct Ion Execselectno (ByVal cmdtext as String, ByVal Cmdtype as CommandType) as DataTable Dim Sqladapter as Sqldataadapt The ER Dim ds as New DataSet ' gives cmd the value of CMD. CommandText = Cmdtext cmd. CommandType = Cmdtype cmd. Connection = conn Sqladapter = New SqlDataAdapter (cmd) Try Sqladapter.fill (DS) Return Ds. Tables (0) Catch ex as Exception Return nothing Finally Call Closecmd (cmd) end Try end Function ' <summary> ' close connection ' </summary> ' <para M name= "conn" ></param> "<remarks></remarks> public Sub Closeconn (ByVal conn as SqlConnection ) IF (Conn. State <> connectionstate.closed) then Conn. Close () conn = Nothing End If end Sub ' <summary> ' close cmd command ' </summary> ' <param name= ' cmd ' ></param> ' <remarks></remarks> public Sub closecmd (ByVal cmd as Sq Lcommand) If not IsNothing (cmd) and then cmd. Dispose () cmd = Nothing End If End SubEnd class</span>
SqlHelper is actually the encapsulation of a series of fixed actions, such as connecting strings in the D layer, creating command commands, performing SQL operations, and so on, which increases the reusability of the code, relieves the writing pressure of the code, and facilitates later code maintenance. SqlHelper more to us a thought, when there is a fixed, repetitive things to do, we can set it as a pattern, every time we do so, we need to do is to deal with the subtle differences, so that both the pressure of our brains, but also to ensure that the success of the matter, Of course, this kind of thought is also in the back of the generic set to get a very good embodiment!
Finally, that sentence, this is only my humble opinion, there is something wrong with the place also ask you to correct me!
Application of SqlHelper in charge system of computer room