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博客 丁國華