VB.NET版機房免費零碎之SqlHelper。本站提示廣大學習愛好者:(VB.NET版機房免費零碎之SqlHelper)文章只能為提供參考,不一定能成為您想要的結果。以下是VB.NET版機房免費零碎之SqlHelper正文
SqlHelper,最早接觸這個詞兒的時分,仿佛是13年的寒假,那個夏天來的比今年來的稍晚一些,呵呵,sqlhelper,翻譯成中文就是數據庫助手,幫手。百度百科這樣對她停止論述:
SqlHelper是一個基於.NET Framework的數據庫操作組件。組件中包括數據庫操作辦法。SqlHelper用於簡化我們反復的去寫那些數據庫銜接(SqlConnection),SqlCommand,SqlDataReader等等。SqlHelper 封裝當時通常是只需求給辦法傳入一些參數如數據庫銜接字符串,SQL參數等,就可以訪問數據庫了,很方便!
可是,好好的,我們為什麼要用SqlHlper?我想百度百科上的解釋曾經很清楚很明白了,SqlHlper是在D層中的代碼籠統出來的,那D層中什麼樣的代碼才干籠統出來?原來啊,就是把那些對數據庫停止增刪改查的操作,存儲進程及順序集等中相反的代碼籠統出來!
在 SqlHelper 類中完成的辦法包括:
ExecuteNonQuery。此辦法用於執行(有參數或無參數的)不前往任何行或值的命令。這些命令通常用於執行數據庫(增\刪\改)更新,但也可用於前往存儲進程的輸入參數。
ExecuteReader。此辦法用於前往 SqlDataReader 對象,該對象包括由某一命令前往的後果集。
ExecuteDataset。此辦法前往 DataSet 對象,該對象包括由某一命令前往的後果集。
前往欄目頁:
上面是SqlHelper的詳細完成:
Imports System.Data.SqlClient Imports System.Configuration Public Class SqlHelper '定義銜接字符串 Dim strConnection As String = System.Configuration.ConfigurationSettings.AppSettings("strConnection") '定義銜接 Dim conn As SqlConnection '定義命令 Dim cmd As SqlCommand '初始化銜接對象 Public Sub New() conn = New SqlConnection(strConnection) End Sub '/// <summary> '/// depiction:<有參數的非查詢的操作> '/// </summary> '/// <param name="<strText>"><增刪改語句或許存儲進程></param> '/// <param name="<cmdType>"><命令類型文本或許存儲進程></param> '/// <param name="<sqlParameter>"><參數數組></param> '/// <returns> '/// <前往布爾值> '/// </returns> Public Function ExecuteNonQuery(ByVal strText As String, ByVal cmdType As CommandType, ByVal sqlParameter As SqlParameter()) As Boolean Dim cmd As New SqlCommand '定義命令 cmd.CommandText = strText 'sql語句或存儲進程名字 cmd.CommandType = cmdType '命令類型是StoredProcedure時,調用存儲進程,普通為CommandText cmd.Connection = conn '銜接數據庫 cmd.Parameters.AddRange(sqlParameter) '傳參 Dim flag As Boolean = False '定義前往值 Try conn.Open() '翻開數據庫銜接 flag = cmd.ExecuteNonQuery cmd.Parameters.Clear() Catch ex As Exception flag = False Finally Call CloseConnection(conn) '封閉數據庫銜接 Call CloseCmd(cmd) End Try Return flag End Function '/// <summary> '/// depiction:<獲取一個帶參數的查詢後果閱讀器> '/// </summary> '/// <param name="<strText>"><增刪改語句或許存儲進程></param> '/// <param name="<cmdType>"><命令類型文本或許存儲進程></param> '/// <param name="<sqlParameter>"><參數數組></param> '/// <returns> '/// <前往布爾值> '/// </returns> Public Function ExecuteReader(ByVal strText As String, ByVal cmdType As CommandType, ByVal sqlParameter As SqlParameter()) As Boolean Dim cmd As New SqlCommand Dim reader As SqlDataReader cmd.CommandText = strText 'sql語句或存儲進程名字 cmd.CommandType = cmdType '命令類型是StoredProcdeure時,調用存儲進程,普通為CommandText cmd.Connection = conn cmd.Parameters.AddRange(sqlParameter) '傳參 Dim flag As Boolean = False '定義前往值 Try conn.Open() reader = cmd.ExecuteReader flag = reader.Read() cmd.Parameters.Clear() Catch ex As Exception flag = False Finally Call CloseConnection(conn) Call CloseCmd(cmd) End Try Return flag End Function '/// <summary> '/// depiction:<獲取一個帶參數的查詢DataTable後果集> '/// </summary> '/// <param name="<strText>"><增刪改語句或許存儲進程></param> '/// <param name="<cmdType>"><命令類型文本或許存儲進程></param> '/// <param name="<sqlParameter>"><參數數組></param> '/// <returns> '/// <前往DataTable> '/// </returns> Public Function ExecuteReaderTable(ByVal strText As String, ByVal cmdType As CommandType, ByVal sqlParameter As SqlParameter()) As DataTable Dim cmd As New SqlCommand '定義命令 Dim dataAdapter As New SqlDataAdapter '定義一個適配器對象 Dim dst As New DataSet Dim dt As New DataTable cmd.CommandText = strText 'sql語句或存儲進程名字 cmd.CommandType = cmdType '命令類型是StoredProcdeure時,調用存儲進程,普通為CommandText cmd.Connection = conn '銜接數據庫 cmd.Parameters.AddRange(sqlParameter) '傳參 Try conn.Open() dataAdapter.SelectCommand = cmd dataAdapter.Fill(dst) dt = dst.Tables(0) Catch ex As Exception Call CloseConnection(conn) Call CloseCmd(cmd) End Try Return dt End Function '/// <summary> '/// depiction:<獲取上機人數> '/// </summary> '/// <param name="<strText>"><增刪改語句或許存儲進程></param> '/// <param name="<cmdType>"><命令類型文本或許存儲進程></param> '/// <param name="<sqlParameter>"><參數數組></param> '/// <returns> '/// <前往整型> '/// </returns> Public Function ExecuteScalar(ByVal cmdText As String, ByVal cmdType As CommandType, ByVal sqlParameter As SqlParameter()) As Integer Dim cmd As New SqlCommand Dim count As Integer cmd.CommandText = cmdText cmd.CommandType = cmdType cmd.Connection = conn cmd.Parameters.AddRange(sqlParameter) Try conn.Open() count = cmd.ExecuteScalar Catch ex As Exception Throw New Exception(ex.Message.ToString()) Finally Call CloseCounection(conn) Call CloseCmd(cmd) End Try Return count End Function '/// <summary> '/// depiction:<算取金額> '/// </summary> '/// <param name="<strText>"><增刪改語句或許存儲進程></param> '/// <param name="<cmdType>"><命令類型文本或許存儲進程></param> '/// <param name="<sqlParameter>"><參數數組></param> '/// <returns> '/// <前往integer> '/// </returns> Public Function ExecuteScalarCash(ByVal cmdText As String, ByVal cmdType As CommandType, ByVal sqlParameter As SqlParameter()) As Decimal Dim cmd As New SqlCommand Dim cash As Decimal cmd.CommandText = cmdText cmd.CommandType = cmdType cmd.Connection = conn cmd.Parameters.AddRange(sqlParameter) Dim i As String Try conn.Open() i = cmd.ExecuteScalar.ToString() If i = "" Then cash = 0.0 Else cash = i End If Catch ex As Exception Throw New Exception(ex.Message.ToString()) Finally Call CloseConnection(conn) Call CloseCmd(cmd) End Try Return cash End Function Public Sub CloseConnection(ByVal conn As SqlConnection) If Not IsNothing(conn.State <> ConnectionState.Closed) Then conn.Close() '封閉銜接 conn = Nothing End If End Sub Private Sub CloseCmd(cmd As SqlCommand) If Not IsNothing(cmd) Then '判別能否為空 cmd.Dispose() cmd = Nothing End If End Sub End Class
SqlHelper封裝成一個類,為開發人員選擇訪問數據庫的方式提供了靈敏性,每種辦法的重載都支持不同的辦法參數,因而開發人員可以確定傳遞銜接、事務和參數信息的方式。像是打包,封裝的思想完滿诠釋,抽離出相反的內容,使代碼失掉復用!
作者:csdn博客 丁國華