Data room charging system-SqlHelper, charging system sqlhelper
I have added a lot of ADO. NET basic knowledge, now it is time to seriously practice it. Next I will introduce the use of SqlHelper IN THE vb.net data center charging system, in the data center charging system, sqlHelper is a self-compiled class that uses simple instances of the objects mentioned above.
SqlHelper: SqlHelper is a NETFramework-based database operation component. The component contains the database operation method. SqlHelper is used to simplify the repeated Writing of database connections (SqlConnection), SqlCommand, SqlDataReader, and so on. After SqlHelper is encapsulated, you only need to input some parameters to the method, such as the database connection string and SQL parameters, to access the database, which is very convenient.
The above is an overview of SqlHelper in Baidu encyclopedia. In the personal refactoring version of The vb.net data center, the SqlHelper class also involves function overloading. The knowledge of function overloading has been used in C ++ before, and I will not describe it too much here. Let's take a look at how the SqlHelper class works!
Contribution of SqlHelper source code for vb.net
<Span style = "font-family: KaiTi_GB2312; font-size: 18px;"> Imports System. dataImports System. data. sqlClientImports System. configuration Public Class SqlHelper 'Call the Configuration file Private ReadOnly strConnection As String = ConfigurationManager. receivettings ("Connstr "). tostring' if the configuration file is not called, use the String to connect to the configuration file, As shown in the following line: 'dim strConnection As String = "Data Source = your machine name; Initial Catalog = JiFang; persist Security Info = True; User ID = sa; Pass Word = 55555 "Public Function ExecuteNonQuery (ByRef plain text As String, ByVal primitive type As CommandType) As Integer Using conn As New SqlConnection (strConnection) 'create a connection object Dim cmd As SqlCommand = conn. createCommand () 'create command object cmd. commandText = plain text 'get the text command to be executed on the data source, insert update delete cmd. commandType = CommandType 'sets the CommandType attribute value of the command object to CommandType, which is an SQL text command. The struct type parameter should be defined at Layer D. Try conn. open () 'open connection Return cmd. executeNonQuery 'catch ex As exception' Catch Exception Return 0 Throw ex 'Throw Exception End Try End Using End function' add, delete, and modify the database with the Public Function ExecuteNonQuery (ByVal plain text As String, byVal parameter type As CommandType, ByVal sqlParams As SqlParameter () As Integer Using conn As New SqlConnection (strConnection) 'defines the connection Dim cmd As SqlCommand = conn. createCommand () 'defines the cmd command c Md. CommandType = argument type cmd. CommandText = plain text cmd. Parameters. AddRange (sqlParams) 'Add the passed Parameters. Try conn. open () Return cmd. executeNonQuery () Catch ex As Exception Return 0 Throw ex End Try End Using End Function 'query operation without the Public Function ExecuteDataTable (ByVal jsontext As String, ByVal jsontype As CommandType) 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' and declares a Dataset object cmd. commandText = plain text 'same as cmd. commandType = primitive type 'same as adp = New SqlDataAdapter (cmd) 'references the Command object retrieved from the data source Try conn. open () 'Open the connection adp. fill (ds) 'fill in the data Return ds in the network dataset. tables (0) 'Return the first table in dataset Catch ex As Exception Return Nothing Throw ex End Try End Using End function' to execute the query operation. 2. The Public Function ExecuteDataTable (ByVal Same text As String, byVal parameter type As CommandType, ByVal sqlParams As SqlParameter () As DataTable Using conn As New SqlConnection (strConnection) Dim cmd As SqlCommand = conn. createCommand () Dim adp As SqlDataAdapter Dim ds As New DataSet cmd. commandText = plain text cmd. commandType = commantype 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>
Let's look at the recharge record query. below is the code for the D Layer of the recharge record.
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 CardNO = @ CardNo" 'query information in the database Dim partition type As CommandType = New CommandType () partition type = CommandType. text Dim helper As New SqlHelper Dim dt As New able defines the queried table set Dim myList As List (Of JFModel. queryRechargeInfo) 'Save the converted generic set dt = helper. executeDataTable (cmdtxet, struct type, sqlparamas) 'execute query' to convert dt to a generic set myList = DataSetToList. converToList (Of JFModel. queryRechargeInfo) (dt) Return myList 'returns an object End FunctionEnd Class
The above is my understanding and application of SqlHelper. I was afraid of seeing SqlHelper before. I didn't know why I wrote the code in it. Now I have prepared some of the previous articles, for SqlHelper, for ADO. NET.