標籤:style os 使用 io 資料 ar cti 代碼 時間
在接受了三層的思想之後,sqlhelper被傳的沸沸揚揚,它給我們的編碼帶來多少優勢、讓編碼者少花了多少時間、多少精力,等等的讚美之詞不絕於耳。自己也是將信將疑的,畢竟自己沒有親身經曆,所以沒有很大的體會。而如今,自己多次使用了sqlhelper,穿梭在各層之間確實也體會到了它帶給我們的簡便之處,結合很多人的版本自己編寫了屬於自己的那一版,以下便是具體代碼:
<span style="font-family:KaiTi_GB2312;font-size:18px;">Imports System.DataImports System.Data.SqlClientImports System.Configuration '在管理器中添加方可使用'''<summary>'''sqlhelper類'''</summary>''' <remarks ></remarks >Public Class SQLHelperDAL '獲得資料庫的串連字串 Private ReadOnly strconnection As String = "server=zhanghui-pc;database=charge-SYS;user id=sa;password=123456" '設定串連 Dim conn As SqlConnection = New SqlConnection(strconnection) '定義cmd命令 Dim cmd As New SqlCommand '''<summary> ''' 執行增、刪、改三個操作(有參), '''</summary> ''' <param name="cmdtext"> </param > ''' <param name="cmdtype"> </param > ''' <param name="paras"> </param > ''' <returns></returns> Public Function ExecAddDelUpdate(ByVal cmdtext As String, ByVal cmdtype As CommandType, ByVal paras As SqlParameter()) As Boolean '將傳入的值分別賦給cmd cmd.Parameters.AddRange(paras) cmd.CommandType = cmdtype cmd.Connection = conn cmd.CommandText = cmdtext Try conn.Open() Return cmd.ExecuteNonQuery() cmd.Parameters.Clear() Catch ex As Exception Return 0 Finally Call closecmd(cmd) Call closeconn(conn) End Try End Function '''<summary> ''' 執行增、刪、改三個操作(無參) ''' </summary> ''' <param name="cmdtext"> </param > ''' <param name="cmdtype"> </param > ''' <returns>intergers </returns > ''' <remarks > </remarks > Public Function ExecAddDelUpdateNo(ByVal cmdtext As String, ByVal cmdtype As CommandType) As Integer '為要執行的cmd命令賦值 cmd.CommandText = cmdtext cmd.CommandType = cmdtype cmd.Connection = conn '執行操作 Try conn.Open() Return cmd.ExecuteNonQuery() cmd.Parameters.Clear() Catch ex As Exception Return 0 Finally Call closecmd(cmd) Call closeconn(conn) End Try End Function '''<summary> ''' 執行查詢操作(有參) ''' </summary> ''' <param name="cmdtext"> </param> ''' <param name="cmdtype"> </param > ''' <param name="paras"> </param > ''' <returns></returns > ''' <remarks></remarks > Public Function ExecSelect(ByVal cmdtext As String, ByVal cmdtype As CommandType, ByVal paras As SqlParameter()) As DataTable Dim sqlAdapter As SqlDataAdapter Dim dt As New DataTable Dim ds As New DataSet '給cmd賦值 cmd.CommandText = cmdtext cmd.CommandType = cmdtype cmd.Connection = conn cmd.Parameters.AddRange(paras) sqlAdapter = New SqlDataAdapter(cmd) '執行個體化adapter對象 Try sqlAdapter.Fill(ds) dt = ds.Tables(0) cmd.Parameters.Clear() Catch ex As Exception MsgBox("查詢失敗", CType(vbOKOnly + MsgBoxStyle.Exclamation, MsgBoxStyle), "警告") Finally Call closecmd(cmd) End Try Return dt End Function ''' <summary> ''' 執行查詢的操作(無參) ''' </summary> ''' <param name="cmdtext"></param> ''' <param name="cmdtype"></param> ''' <returns></returns> ''' <remarks></remarks> Public Function ExecSelectNo(ByVal cmdtext As String, ByVal cmdtype As CommandType) As DataTable Dim sqlAdapter As SqlDataAdapter Dim ds As New DataSet '給cmd賦值 cmd.CommandText = cmdtext cmd.CommandType = cmdtype cmd.Connection = conn sqlAdapter = New SqlDataAdapter(cmd) Try sqlAdapter.Fill(ds) Return ds.Tables(0) Catch ex As Exception Return Nothing Finally Call closecmd(cmd) End Try End Function ''' <summary> ''' 關閉串連 ''' </summary> ''' <param name="conn"></param> ''' <remarks></remarks> Public Sub closeconn(ByVal conn As SqlConnection) If (conn.State <> ConnectionState.Closed) Then conn.Close() conn = Nothing End If End Sub ''' <summary> ''' 關閉cmd命令 ''' </summary> ''' <param name="cmd"></param> ''' <remarks></remarks> Public Sub closecmd(ByVal cmd As SqlCommand) If Not IsNothing(cmd) Then cmd.Dispose() cmd = Nothing End If End SubEnd Class</span>
sqlhelper其實是對D層中連接字串、建立command命令、執行sql操作等一系列固定動作的封裝,它增加了代碼的複用性、減輕了代碼的書寫壓力,同時有利於後期的代碼維護。sqlhelper更傳遞給我們一種思想,當有固定的、重複的事情要去做的話,我們可以把它定為一個模式,每次都這麼去做,我們需要做的只是去處理那邊細微的差別,這樣既減輕了我們大腦的壓力,同時又保證了事情的成功度,當然這種思想也在後邊的泛型集合中得到了很好的體現!
最後還是那句話,這隻是鄙人的小小見解,有什麼不妥的地方還請大家多多指正!
機房收費系統中sqlhelper的應用