在Visual Studio 2013中使用Access数据库

时间:2014-06-24 20:07:06

标签: database vb.net ms-access visual-studio-2013

我一直试图找到一种方法来使用Visual Basic来处理Access数据库,但是我在互联网上看到的所有库(ADODB等)都不存在于VS2013或者没有我想要使用的所有功能,比如Recordset对象(OleDb就是这样一个库)。这只是'你需要安装正确的库'的情况吗?或者我在使用Microsoft数据库时错过了一些新标准?

2 个答案:

答案 0 :(得分:1)

事实证明,我实际上是在使用错误的类。页面here(感谢@Plutonix)引用了DataSet11和OleDbAdapter1对象,这些对象在结合使用时似乎具有我在旧DAO和ADO Recordset对象中看到的那种功能。

答案 1 :(得分:0)

我有一个使用VB.NET的ASP.NET网站,一个简单的Microsoft Access 2000数据库后端。我实际上编写了自己的自定义类来从数据库中获取某些表和查询以及数据,它基于OleDb。它不是最好的代码,实际上有点可耻。但它完成了我用它的简单应用程序的工作。

剪切文件AccessDatabase.vb的版本

Imports Microsoft.VisualBasic
Imports System.Data.OleDb

Public Class AccessDatabase
    Friend db As New OleDbConnection
    Private sPath As String

    Public Sub New(ByRef sPath As String)
        GetDatabase(sPath)
    End Sub

    'Use Server.MapPath("App_Data\WebContent.mdb") to load the database.
    Private Function GetDatabase(ByRef sPath As String) As OleDbConnection
        Try
            If db.State <> System.Data.ConnectionState.Open Then
                db = New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & sPath)
                db.Open()
            End If
        Catch e As Exception
            Throw New Exception("Exception encountered when attempting to open database " & sPath, e)
        End Try
        Return db
    End Function

    Public Function GetTable(ByRef sTableName As String, Optional ByRef sWhere As String = "", Optional ByRef sSort As String = "") As AccessData
        Dim a As New AccessData(Me)
        a.SetTable(sTableName, sWhere, sSort)
        Return a
    End Function

    Public Sub Close()
        If db.State <> Data.ConnectionState.Closed Then
            db.Close()
        End If
    End Sub

    Protected Overrides Sub Finalize()
        Try
            If Not db Is Nothing Then
                If db.State <> Data.ConnectionState.Closed Then
                    db.Close()
                End If
            End If
        Catch ex As Exception

        End Try
    End Sub
End Class

剪切File AccessData.vb的版本

Imports Microsoft.VisualBasic
Imports System.Data.OleDb

Public Class AccessData

    Private db As AccessDatabase
    Private data As OleDbDataReader

    Public Sub New(ByRef d As AccessDatabase)
        SetDB(d)
    End Sub

    Public Sub New(ByRef d As AccessDatabase, ByRef sTableName As String, Optional ByRef sWhere As String = "", Optional ByRef sSort As String = "")
        SetDB(d)
        SetTable(sTableName, sWhere, sSort)
    End Sub

    Public Sub SetDB(ByRef d As AccessDatabase)
        db = d
    End Sub

    Public Sub SetTable(ByRef sTableName As String, Optional ByRef sWhere As String = "", Optional ByRef sSort As String = "")
        If sWhere = "" And sSort = "" Then
            SetFromQuery("SELECT " & sTableName & ".* FROM " & sTableName)
        ElseIf sSort = "" Then
            SetFromQuery("SELECT " & sTableName & ".* FROM " & sTableName & " WHERE " & sWhere)
        ElseIf sWhere = "" Then
            SetFromQuery("SELECT " & sTableName & ".* FROM " & sTableName & " ORDER BY " & sSort)
        Else
            SetFromQuery("SELECT " & sTableName & ".* FROM " & sTableName & " WHERE " & sWhere & " ORDER BY " & sSort)
        End If
    End Sub

    Public Sub SetFromQuery(ByRef sQuery As String)
        Dim c As OleDbCommand
        c = New OleDbCommand(sQuery, db.db)
        data = c.ExecuteReader()
    End Sub

    'Returns the value of the requested field of the current row of the reader
    Public Function GetValue(ByRef sField As String) As String
        Dim iOrdinal As Integer

        Try
            iOrdinal = data.GetOrdinal(sField)
            If Not data.GetValue(iOrdinal).Equals(DBNull.Value) Then
                Return data.GetValue(iOrdinal).ToString()
            End If
        Catch e As System.IndexOutOfRangeException
            Throw New System.IndexOutOfRangeException("Field '" & sField & "' was requested from " & vbCrLf & sQuery & "," & vbCrLf & "but it does not exist.", e)
        Catch e As System.InvalidOperationException
            Throw New System.InvalidOperationException("No data exists for the current row in field '" & sField & "'.  Make sure you have performed a Read() operation and that you are not at the EOF or BOF of the data stream.", e)
        End Try

        Return ""
    End Function

    'This will close the stream when data.Read() returns false
    Public Function Read() As Boolean
        Dim bResult As Boolean
        bResult = data.Read()
        If Not bResult Then
            data.Close()
        End If
        Return bResult
    End Function
End Class