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

转载 2015年07月10日 09:58:57

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数据库帮助类

VB.net 操作Access数据库的一些常用方法,代码如下 Imports System Imports System.Data.SqlClient Imports System.Config...

VB.NET 使用 OleDb 操作 Access 数据库(来自 MSDN)

VB.NET 使用 OleDb 操作 Access 数据库 这里采用 OleDb 方式对 Access 数据库进行访问,主要内容都来自 MSDN 帮助文档。建议新手注意 MSDN 的用途。 首先导入命...

VB.NET操作ACCESS数据库读取存入图片

一、准备工作     在ACCESS数据库中,将字段声明为 OLE链接 类型 二、将图片框中图片转化成二进制数组的方法         Dim MyStream As New System.IO....

vb.net操作数据库之ACCESS(一)

转载自:http://blog.csdn.net/machong8183/article/details/5488191 一直想写下数据库操作方面的,只是没什么时间(好像是借口,应该是懒或者没心情吧...

vb.net操作access数据库实例

  • 2009-08-05 16:39
  • 116KB
  • 下载

asp.net学习之路之登录+access数据库操作

先来一组图,看看界面到是怎么样的吧 登录界面,做的相当搓,没做美工。笔者在之前的博文中也有说过,希望不要吐槽哈 管理员界面: 还有个非管理员的界面。 与管理员界面差不多,管理员是可以直接 ...

access数据库连接VB.NET类

内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:深度学习:神经网络中的前向传播和反向传播算法推导
举报原因:
原因补充:

(最多只允许输入30个字)