Database 在Visual Studio 2013中使用Access数据库
我一直在试图找到一种使用Visual Basic处理Access数据库的方法,但我在互联网上看到的所有库(ADODB等)要么在VS2013中不存在,要么没有我想要使用的所有功能,比如记录集对象(OleDb就是这样的库之一)。这只是“您需要安装正确的库”的情况吗?还是我缺少了一些使用Microsoft数据库的新标准?我有一个使用VB.NET的ASP.NET网站,它是一个简单的Microsoft Access 2000数据库后端。实际上,我编写了自己的自定义类来从数据库中获取某些表、查询和数据,它基于OleDb。这不是最好的代码,实际上有点丢脸。但它完成了我使用它的简单应用程序的工作 文件AccessDatabase.vb的剪切版本Database 在Visual Studio 2013中使用Access数据库,database,vb.net,ms-access,visual-studio-2013,Database,Vb.net,Ms Access,Visual Studio 2013,我一直在试图找到一种使用Visual Basic处理Access数据库的方法,但我在互联网上看到的所有库(ADODB等)要么在VS2013中不存在,要么没有我想要使用的所有功能,比如记录集对象(OleDb就是这样的库之一)。这只是“您需要安装正确的库”的情况吗?还是我缺少了一些使用Microsoft数据库的新标准?我有一个使用VB.NET的ASP.NET网站,它是一个简单的Microsoft Access 2000数据库后端。实际上,我编写了自己的自定义类来从数据库中获取某些表、查询和数据,它基
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
事实上,我使用了错误的课程。页面(感谢@puropoix)引用了DataSet11和OleDbAdapter1对象,当它们结合使用时,似乎具有我在旧DAO和ADO记录集对象中看到的那种功能。以及@puropoix建议的功能。这两个链接都会引导您找到答案。
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