用于VB.NET的SQL数据库连接类

Imports Microsoft.VisualBasic
Imports System.Data
Imports System.Data.SqlClient

Public Class dataBase
    Public cnStr As String
    Private _conn As New SqlClient.SqlConnection
    Private _cmd As New SqlClient.SqlCommand
    Private _sql As String

    Public Function getConnectString() As String '返回连接数据库字符串
        cnStr = System.Configuration.ConfigurationManager.AppSettings("cnstr")
        Return cnStr
    End Function

    Public Sub Open() '打开数据库连接
        Dim connStr As String
        connStr = getConnectString()
        _conn = New SqlConnection(connStr)
        _conn.Open()
    End Sub

    Public Sub Close() '关闭数据库连接
        _conn.Dispose()
        _conn.Close()
    End Sub

    Public Sub RunSql(ByVal sql As String) '执行SQL语句
        Open()
        Dim cmd As New SqlCommand(sql, _conn)
        cmd.ExecuteNonQuery()
        Close()
    End Sub

    Public Function getDataSet(ByVal sql As String) As DataSet '返回Dataset
        Open()
        Dim rs As New SqlDataAdapter(sql, _conn)
        Dim ds As New DataSet
        rs.Fill(ds)
        Return ds
    End Function

End Class

 

posted on 2010-05-07 17:22  icycore  阅读(2813)  评论(0编辑  收藏  举报

导航