機房收費系統中sqlhelper的應用

來源:互聯網
上載者:User

標籤: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的應用

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.