Computer room charge System--sqlhelper

Source: Internet
Author: User

Before oneself will be a lot of the basic knowledge of ADO, now this earnest practice, the following I introduce the vb.net version of the computer room fee system, the use of SqlHelper, in the computer room charges, SqlHelper is a class of their own, the use of a few objects in front of a simple example.

SqlHelper Brief Introduction: SqlHelper is a based on · The NETFramework database operations component. The component contains a database action method. SqlHelper is used to simplify the way you repeatedly write those database connections (SqlConnection), Sqlcommand,sqldatareader, and so on. SqlHelper package is usually only needed to pass some parameters such as database connection string, SQL parameters, etc., you can access the database, very convenient.

The above is the Baidu Encyclopedia in the SqlHelper of the introduction. In the personal refactoring version of the vb.net room, the SqlHelper class also involves overloading the function, the knowledge of function overloading has been approached in C + +, and there is no excessive description. Let's take a look at how sqlhelper this class works!

VB.net version SqlHelper source dedicated

<span style= "FONT-FAMILY:KAITI_GB2312;FONT-SIZE:18PX;"    >imports system.dataimports System.Data.SqlClientImports system.configuration public Class SqlHelper ' invoke configuration file Private ReadOnly strconnection as String = Configurationmanager.appsettings ("ConnStr"). ToString ' If you do not call the configuration file, connect with the string as follows ' Dim strconnection As String = ' Data source= its own machine name; Initial Catalog=jifang; Persist Security info=true; User Id=sa;        password=55555 "Public Function ExecuteNonQuery (ByRef cmdtext as String, ByVal Cmdtype as CommandType) as Integer Using Conn as New SqlConnection (strconnection) ' Creates a Connection object Dim cmd as SqlCommand = conn. CreateCommand () ' Creates a command object cmd. CommandText = Cmdtext ' gets the text command to execute on the data source, insert update delete cmd. CommandType = Cmdtype ' Sets the CommandType property value of the command object to Cmdtype, which is the SQL Text command.            The Cmdtype parameter should be defined on the D layer. Try Conn. Open () ' Opens the connection to Return cmd. ExecuteNonQuery ' operation of the database catch ex as Exception ' catch exception returN 0 throw ex ' throws an exception ' end Try end Using End Function ' to make a database add-and-revise operation parameters Public fun         Ction ExecuteNonQuery (ByVal cmdtext as String, ByVal Cmdtype as CommandType, ByVal Sqlparams as SqlParameter ()) as Integer The Using conn as New SqlConnection (strconnection) ' Defines the connection Dim cmd as SqlCommand = conn. CreateCommand () ' Defines the cmd command cmd.commandtype = cmdtype cmd.commandtext = cmdtext cmd.            Parameters.addrange (sqlparams) ' adds the passed in parameters. Try Conn. Open () Return cmd.        ExecuteNonQuery () Catch ex as Exception Return 0 Throw ex End Try End Using End Function ' query operation parameterless public Function executedatatable (ByVal cmdtext as String, ByVal Cmdtype as C Ommandtype) as DataTable Using conn as New SqlConnection (strconnection) Dim cmd as SqlCommand = conn. CreateCommand () Dim ADP as SqlDataAdapter ' declares a dataadapter Object Dim DS as New DataSet ' declares a DataSet object cmd. CommandText = Cmdtext ' Ibid. cmd. CommandType = Cmdtype ' above ADP = New SqlDataAdapter (cmd) ' reference to the Command object retrieved from the data source Try Conn . Open () ' Opens the connection to ADP. Fill (DS) ' Network dataset to populate the data Return ds.            Tables (0) ' Returns the first table in a DataSet Catch ex as Exception return nothing Throw ex End Try End Using End Function ' Perform a query operation 2 parameters public Function executedatatable (ByVal cmdtext as String, B Yval Cmdtype as CommandType, ByVal Sqlparams as SqlParameter ()) as DataTable Using conn as New SqlConnection (Strcon nection) Dim cmd as SqlCommand = conn. CreateCommand () Dim adp As SqlDataAdapter Dim ds as New DataSet cmd. CommandText = Cmdtext cmd. CommandType = Cmdtype cmd.    Parameters.addrange (sqlparams) ADP = New SqlDataAdapter (cmd) Try            Conn. Open () ADP. Fill (DS) Return ds.        Tables (0) Catch ex as Exception Return nothing Throw ex End Try End Using End FunctionEnd class</span>
take the query recharge record, the following is the top-up record D layer code.

imports System.dataimports System.Data.SqlClientPublic class QueryRechargeInfo1 public Function Querycharge (model as Jfmodel.queryrechargeinfo) as IList (of Jfmodel.queryrechargeinfo) Dim Sqlparamas as SqlParameter () = {New SqlParameter ("@CardNo", model. Cardno)} Dim cmdtxet As String = "SELECT * from Recharge_info where [email protected]" to query information in database Dim C Mdtype as CommandType = new CommandType () Cmdtype = CommandType.Text Dim helper As New SqlHelper Dim DT as New DataTable ' definition query to the set of tables Dim myList as List (of Jfmodel.queryrechargeinfo) ' Save converted generic Set dt = h Elper. Executedatatable (Cmdtxet, Cmdtype, Sqlparamas) ' Execute query ' converts DT to generic set myList = Datasettolist.convertolist (O F jfmodel.queryrechargeinfo) (DT) return myList ' returns an entity End FunctionEnd Class 
The above is my understanding and application of SqlHelper, think before see SqlHelper fear, inside of the code what do not know why so write, now have a few articles in front of pave, to SqlHelper, to ADO finally a little understand.

Computer room charge System--sqlhelper

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.