vb.net 操作Access数据库帮助类

VB.net 操作Access数据库的一些常用方法,代码如下:

Imports System
Imports System.Data.SqlClient
Imports System.Configuration
Imports System.Collections.Generic
Imports System.Text
Imports System.Data
Imports System.Configuration.ConfigurationSettings
Imports System.Data.OleDb

Public Class DB

    Private Shared com As OleDb.OleDbCommand
    Private Shared reader As OleDb.OleDbDataReader
    Private Shared adapter As OleDb.OleDbDataAdapter
    Private Shared conn As OleDb.OleDbConnection

    ''' <summary>
    ''' 获取数据库连接
    ''' </summary>
    ''' <value></value>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Shared ReadOnly Property NewConn() As OleDbConnection
        Get
            Dim connectionString As String
            'connectionString = System.Configuration.ConfigurationSettings.GetConfig("Supermarket")
            '连接2010数据库
            'connectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=F:\GCU.accdb"
            '连接03-07数据库
            connectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=F:\JL\GCU.MDB"

            '应该在这里先判断conn是否为Nothing
            If conn Is Nothing Then
                conn = New OleDb.OleDbConnection(connectionString)
            End If
            If conn.State <> ConnectionState.Open Then
                conn.Open()
            End If
            Return conn
        End Get
    End Property

    ''' <summary>
    ''' 执行增删改(无参)
    ''' </summary>
    ''' <param name="sql">执行的Sql语句</param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Shared Function ExecuteNonQuery(ByVal sql As String) As Integer
        com = New OleDb.OleDbCommand(sql, NewConn())
        Return com.ExecuteNonQuery()
    End Function

    ''' <summary>
    ''' 执行增删改(有参)
    ''' </summary>
    ''' <param name="sql"></param>
    ''' <param name="para"></param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Shared Function ExecuteNonQuery(ByVal sql As String, ByVal para As OleDbParameter()) As Integer
        com = New OleDb.OleDbCommand(sql, NewConn)
        com.Parameters.AddRange(para)
        Return com.ExecuteNonQuery()
    End Function

    ''' <summary>
    ''' 执行增删改的存储过程
    ''' </summary>
    ''' <param name="para"></param>
    ''' <param name="ProcedureName"></param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Shared Function ExecuteNonQuery(ByVal para As SqlParameter(), ByVal ProcedureName As String) As Integer
        Dim cmd As OleDb.OleDbCommand
        cmd = New OleDb.OleDbCommand()
        cmd.Connection = NewConn()
        cmd.CommandText = ProcedureName
        cmd.CommandType = CommandType.StoredProcedure
        cmd.Parameters.AddRange(para)
        Return com.ExecuteNonQuery
    End Function

    ''' <summary>
    ''' 执行查询(返回一个结果集,无参)
    ''' </summary>
    ''' <param name="sql"></param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Shared Function GetScalar(ByVal sql As String) As String
        Dim dataset As DataSet
        dataset = New DataSet()
        com = New OleDb.OleDbCommand(sql, NewConn)
        adapter = New OleDbDataAdapter(com)
        adapter.Fill(dataset)
        If dataset.Tables.Count > 0 And dataset.Tables(0).Rows.Count > 0 Then
            Return dataset.Tables(0).Rows(0)(0).ToString()
        End If
        Return "Null"
    End Function

    ''' <summary>
    ''' 执行查询(返回一个结果集,有参)
    ''' </summary>
    ''' <param name="sql"></param>
    ''' <param name="para"></param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Shared Function GetScalar(ByVal sql As String, ByVal para As SqlParameter()) As Integer
        com = New OleDb.OleDbCommand(sql, NewConn)
        com.Parameters.AddRange(para)
        Return Convert.ToInt32(com.ExecuteScalar())
    End Function

    ''' <summary>
    ''' 执行查询(返回一行数据,无参)
    ''' </summary>
    ''' <param name="sql"></param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Shared Function GetReader(ByVal sql As String) As OleDbDataReader
        com = New OleDb.OleDbCommand(sql, NewConn)
        reader = com.ExecuteReader()
        Return reader
    End Function

    ''' <summary>
    ''' 执行查询(返回一行数据,有参)
    ''' </summary>
    ''' <param name="sql"></param>
    ''' <param name="para"></param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Shared Function GetReader(ByVal sql As String, ByVal para As SqlParameter()) As OleDbDataReader
        com = New OleDb.OleDbCommand(sql, NewConn)
        com.Parameters.AddRange(para)
        reader = com.ExecuteReader()
        Return reader
    End Function

    ''' <summary>
    ''' 执行查询(返回一个数据集,无参)
    ''' </summary>
    ''' <param name="sql"></param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Shared Function GetDataSet(ByVal sql As String) As DataTable
        Dim dataset As DataSet
        dataset = New DataSet()
        com = New OleDb.OleDbCommand(sql, NewConn)
        adapter = New OleDbDataAdapter(com)
        adapter.Fill(dataset)
        Return dataset.Tables(0)
    End Function


    ''' <summary>
    ''' 执行查询(返回一个数据集,有参)
    ''' </summary>
    ''' <param name="sql"></param>
    ''' <param name="para"></param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Shared Function GetDataSet(ByVal sql As String, ByVal para As SqlParameter()) As DataTable
        Dim dataset As DataSet
        dataset = New DataSet()
        com = New OleDbCommand(sql, NewConn)
        com.Parameters.AddRange(para)
        adapter = New OleDbDataAdapter(com)
        adapter.Fill(dataset)
        Return dataset.Tables(0)
    End Function
End Class
阅读更多
文章标签: vb-net access数据库
个人分类: VS.NET学习
想对作者说点什么? 我来说一句

access数据库连接VB.NET

2013年06月26日 6KB 下载

vb.net操作access数据库实例

2009年08月05日 116KB 下载

VB.Net访问Access数据库操作示例

2009年06月05日 139KB 下载

VB.NET 通过语句 读写ACCESS数据库

2017年02月25日 808KB 下载

没有更多推荐了,返回首页

不良信息举报

vb.net 操作Access数据库帮助类

最多只允许输入30个字

加入CSDN,享受更精准的内容推荐,与500万程序员共同成长!
关闭
关闭