vb.net操作SQL数据库比较好用的类。

在网上发现的,一直在用,感觉不错。忘记是哪位老大搞的了,多谢!

 

Public Class ClassDB
    Public cn As System.Data.SqlClient.SqlConnection
    Public da As System.Data.SqlClient.SqlDataAdapter
    Public ds As New DataSet
    Public dt As New DataTable
    Public cmd As System.Data.SqlClient.SqlCommand

    '****************************************************************************************
    '连接数据库
    Public Function ConnectionOpen(ByVal server As String, ByVal DBName As String, ByVal uid As String, ByVal pwd As String) As Boolean 'ByVal server As String, ByVal DBName As String, ByVal uid As String, ByVal pwd As String
        Dim cnstr As String = "Persist Security Info=False;" _
                              & "Server=" & server & ";" _
                              & "Database=" & DBName & ";" _
                              & "User ID=" & uid & ";" _
                              & "Password=" & pwd
        cn = New System.Data.SqlClient.SqlConnection(cnstr)
        Try
            cn.Open()
        Catch ex As System.Data.SqlClient.SqlException
            MsgBox(ex.Message.ToString())
        End Try
    End Function

    '断开数据库的连接
    Public Function ConnectionClose() As Boolean
        Try
            cn.Close()
        Catch ex As System.Data.SqlClient.SqlException
            MsgBox(ex.Message.ToString())
        End Try
    End Function

    '插入记录、删除记录、更新记录到指定的数据库->数据表
    Public Function InsertDelUpdateRecord(ByVal cmdText As String) As Boolean
        Try
            cmd = New System.Data.SqlClient.SqlCommand(cmdText, cn) '指定sql命令及该命令用于哪一个数据库连接
            cmd.ExecuteNonQuery()
        Catch ex As Exception
            MsgBox(ex.Message.ToString())

        End Try
    End Function

    '填充数据集,他会根据cmdtext参数说指定的查询返回数据库里特定的表
    Public Function DataSetFill(ByVal cmdText As String, ByVal tabName As String) As Boolean
        Try
            cmd = New System.Data.SqlClient.SqlCommand(cmdtext, cn) '指定sql查询命令及该命令用于哪一个数据库连接
            da = New System.Data.SqlClient.SqlDataAdapter(cmd)
            da.Fill(ds, tabName) '表名可以映射为数据库中实际的表名称,但是他和数据库实际的表名称不是一回事
        Catch ex As Exception
            MsgBox(ex.Message.ToString())
        End Try
    End Function

    '调用存储过程
    Public Function ExecProc(ByVal procName As String, ByVal tabName As String) As Boolean
        Try
            cmd = New System.Data.SqlClient.SqlCommand(procName, cn)
            cmd.CommandType = CommandType.StoredProcedure
            da = New System.Data.SqlClient.SqlDataAdapter(cmd)
            da.Fill(ds, tabName)
        Catch ex As Exception
            MsgBox(ex.Message.ToString())
        End Try
    End Function

    '这个基本不用,因为可以直接通过ds来调用
    Public Function DataTableFill(ByVal index As Integer) As Boolean
        Try
            dt = ds.Tables(index)
        Catch ex As Exception
            MsgBox(ex.Message.ToString())
        End Try
    End Function

    '这个基本不用,因为可以直接通过ds来调用
    Public Function DataTableFill(ByVal name As String) As Boolean
        Try
            dt = ds.Tables(name)
        Catch ex As Exception
            MsgBox(ex.Message.ToString())
        End Try
    End Function

    '释放资源
    Public Function DBDispos() As Boolean
        Try
            cn.Dispose()
            'da.Dispose()
            ds.Dispose()
            dt.Dispose()
        Catch ex As Exception
            MsgBox(ex.Message.ToString())
        End Try

    End Function

End Class

你可能感兴趣的:(vb.net)