2014-06-24 2 views
0

Visual Basic을 사용하여 Access 데이터베이스를 사용하는 방법을 알아 내려고 노력했지만이 사이트에서 참조한 모든 라이브러리 (ADODB 등) 인터넷은 VS2013에 존재하지 않거나 Recordset 개체 (OleDb는 그러한 라이브러리 중 하나)와 같은 모든 기능을 사용하지 않습니다. 이것은 '올바른 라이브러리를 설치해야합니까?'의 경우입니까? 아니면 Microsoft 데이터베이스로 작업하면서 새로운 표준을 놓치고 있습니까?Visual Studio 2013에서 Access 데이터베이스 작업

+2

http://support.microsoft.com/kb/821765 – Plutonix

+0

http://social.msdn.microsoft.com/Forums/vstudio/en-US/5e917e8f-6d39-4226-8940-bf9560f78d1d/vs-2013 -connection-to-access-database? forum = vbgeneral @Plutonix가 제안한 것과 함께. 두 링크 모두 당신을 당신의 답으로 인도합니다. –

답변

1

그것은 잘못된 클래스를 사용하여, 사실, 내가이었다 밝혀졌습니다. here (Thanks @Plutonix)은 DataSet11 및 OleDbAdapter1 개체에 대한 참조를 만듭니다.이 개체를 함께 사용하면 이전 DAO 및 ADO Recordset 개체에서 본 일종의 기능이있는 것처럼 보입니다.

0

간단한 Microsoft Access 2000 데이터베이스 백엔드 인 VB.NET을 사용하는 ASP.NET 웹 사이트가 있습니다. 실제로 데이터베이스에서 특정 테이블과 쿼리 및 데이터를 가져 오기 위해 자체 사용자 지정 클래스를 작성했으며 OleDb를 기반으로합니다. 사용할 수있는 최고의 코드는 아니며 사실 조금 부끄럽습니다. 하지만 그것을 사용하는 간단한 응용 프로그램에 대한 작업이 완료됩니다. 파일 AccessData.vb의 파일 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 

컷 버전은

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 
관련 문제