Vb. NET computer room charge system SqlHelper

Source: Internet
Author: User

have been using SqlHelper, but until now a little understanding of the magic of the data connection class really embodies the code reuse.

Update for SQL statements insert Find Delete
Include parametric, parameterless update insert find delete includes

The following is a detailed explanation of the code, now look at the understanding, wait until later to write more convenient.

"'
Imports System.Data
Imports System.Data.SqlClient
Imports System.Configuration ' must add a reference to the manager
”’
"' The SqlHelper class is a SQL data operation dedicated to a wide range of users for high performance, upgradeable and best practices.
”’
”’
Public Class SqlHelper
' Define variables
' Get the connection string for the database
Private ReadOnly strconnection as String = Configurationmanager.appsettings ("ConnStr")
' Set connection
Dim conn as SqlConnection = New SqlConnection (strconnection)
' Define CMD command
Dim cmd as New SqlCommand

"<summary>" to perform additions and deletions to three operations, (with a parameter) The return value is a Boolean type, confirm whether the execution succeeds ' </summary> ' <param name= "Cmdtext" > Need to execute Statements, typically SQL statements, and stored procedures </param> "<param name=" Cmdtype "> Determine the type of SQL statements, generally not stored procedures </param> ' <param Name= "Paras" > parameter array, unable to confirm how many parameters </param> ' <returns></returns> ' <remarks></remarks > Public Function execadddelupdate (ByVal cmdtext as String, ByVal Cmdtype as CommandType, ByVal paras as SqlParameter ( ) as Integer to the value of the passed-in values, respectively, for the Cmd property assignment cmd. Parameters.addrange (paras) ' parameters passed in Cmd.commandtype = Cmdtype ' Set a value explaining Cmdtext cmd. Connection = Conn ' Set connection, global variable cmd.commandtext = Cmdtext ' Set the query's statement Try Conn. Open () ' Opens the connection to Return cmd. ExecuteNonQuery () ' perform additions and deletions and change operation CMD. Parameters.clear () ' Clear parameter Catch ex as Exception return 0 ' If there is an error, return 0 Final Ly call CLOSECONN (conn) call Closecmd (cmd) End tryend Function ' <summary> ' to perform additions and deletions three operations, (no reference) ' </summary> ' <param name= ' cm  Dtext "> needs to execute statements, typically SQL statements, and stored procedures </param>" <param name= "Cmdtype" > Determine the type of SQL statement, generally not a stored procedure </param> "<returns>interger, number of rows affected </returns>" <remarks>2013 February 2 8:19:59</remarks> Public Function Execadddelupdateno (ByVal cmdtext as String, ByVal Cmdtype as CommandType) as Integer ' is the command cmd to execute cmd. CommandText = Cmdtext ' first query of SQL statement cmd. CommandType = Cmdtype ' Sets how the SQL statement interprets CMD. Connection = conn ' Set connection ' to execute action Try Conn. Open () Return cmd. ExecuteNonQuery () Catch ex as Exception Return 0 Finally call CLOSECONN (conn) Call Closecmd (cmd End tryend Function ' <summary> ' performs the operation of the query (with reference), the parameters are not limited to ' ' </summary> ' ' <param name= ' Cmdtext ' > You need to execute statements, typically SQL statements, and stored procedures </param> "<param name=" Cmdtype "> Determine the type of SQL statement, which is generally not a stored procedure </pAram> "<param Name=" paras "> Incoming parameters </param> ' <returns></returns> ' <remarks> </remarks> public Function execselect (ByVal cmdtext as String, ByVal Cmdtype as CommandType, ByVal paras as Sqlpara       Meter ()) as DataTable Dim sqladapter As SqlDataAdapter Dim dt As New DataTable Dim ds As New DataSet ' or cmd assignment Cmd.commandtext = Cmdtext Cmd.commandtype = cmdtype cmd. Connection = conn cmd. Parameters.addrange (paras) ' parameter added Sqladapter = New SqlDataAdapter (cmd) ' Instantiation adapter Try Sqladapter.fill (d s) ' Use adapter to populate the dataset with the If DS. Tables.count = 0 Then Else dt = ds. Tables (0) ' DataTable is the first table cmd of a dataset. Parameters.clear () ' Clear parameter End If Catch ex as Exception MsgBox ("Query Failed", CType (vbOKOnly + msgboxstyl E.exclamation, MsgBoxStyle), "warning") finally ' must destroy the cmd call Closecmd (cmd) End Tr Y Return DtEnd Function ' <summary> ' performs the operation of the query, (no reference) ' </summary> ' <param name= "Cmdtext" > need to execute statements, typically SQL statements, also Stored procedure </param> "<param name=" Cmdtype "> determines the type of SQL statement, generally not a stored procedure </param> ' <returns>datatable, Query to Table </returns> ' ' <remarks></remarks> public Function execselectno (ByVal cmdtext as String, ByVal C Mdtype as CommandType) as DataTable Dim sqladapter As SqlDataAdapter Dim ds As New DataSet ' or cmd-value cmd. CommandText = Cmdtext cmd. CommandType = Cmdtype cmd. Connection = conn Sqladapter = New SqlDataAdapter (cmd) ' Instantiation adapter Try Sqladapter.fill (DS) ' with Ada Pter populates the dataset with the Return DS.                            Tables (0) ' DataTable is the first table of a DataSet Catch ex as Exception Return nothing Finally ' Finally, be sure to destroy the cmd call Closecmd (cmd) end tryend Function ' <summary> ' ' Close connection ' &LT;/SUMMARY&G  T "' <param name=" conn "> need to close the connection </param> ' &Lt;remarks></remarks> public Sub Closeconn (ByVal conn as SqlConnection) IF (Conn. State <> connectionstate.closed) and then ' if Conn is not turned off. Close () ' Close Connection conn = Nothing ' does not point to the original object End ifend Sub ' ' <summary> ' ' Close command ' </summary> ' <param name= ' cmd ' > command to close </param> ' <remarks></remarks> Pub Lic Sub closecmd (ByVal cmd as SqlCommand) if not isnothing (cmd) Then ' if cmd command exists. Dispose () ' Destroy cmd = Nothing End ifend SubEnd Class
This database link class is very comprehensive and can query both SQL statements and view stored procedures. The trigger has not been tried, do not know how?

Vb. NET 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.