Reading tables using OleDBConnection

非 Y 不嫁゛ 提交于 2019-12-24 16:14:21

问题


First off, i'm new to VB and this is my first project using OleDBConnection.

ok, so i'm trying to the most simple thing using oleDbConnection (i assume). I just want to read data from a table in the Access DB and display that information to dropboxes (or anything) in my winForm.

Public Class QueManger
Dim dbConnection As OleDbConnection
Dim dbCommand As OleDbCommand
Dim dbDataAdapter As OleDbDataAdapter
Dim ConnectString As String = "Provider = Microsoft.Jet.OLEDB.4.0;" & "Data Source = \\atrts10\F:\Applications\ATRTaxCert\Development\mtaylor\TaxCert_be_test.accdb"
Dim dtMain As DataTable

Private Sub QueManger_Load(sender As Object, e As EventArgs) Handles MyBase.Load

    StatusName()
End Sub

Private Sub StatusName()
    Dim taxconn As OleDbConnection
    Try

        taxconn = New OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=\\atrts10\F:\Applications\ATRTaxCert\Development\mtaylor\TaxCert_be_test.accdb")
        Dim taxcmd As OleDbCommand = taxconn.CreateCommand
        taxcmd.CommandText = "SELECT StatusName FROM Status ORDER BY StatusName"
        Dim rdr2 As OleDbDataReader
        If taxconn.State = ConnectionState.Closed Then
            taxconn.Open()
        End If
        rdr2 = taxcmd.ExecuteReader

        'boxStatus.Items.Add("All")
        While rdr2.Read()
            boxClient.Items.Add(rdr2.Item("StatusName"))
        End While
    Catch ex As Exception

    Finally
        taxconn.Close()
    End Try
End Sub

The error comes when it tries to run the "taxconn.Open()" function.

The error says "The Microsoft Access database engine cannot open or write to the file '\atrts10\F:\Applications\ATRTaxCert\Development\mtaylor\TaxCert_be_test.accdb'. It is already opened exclusively by another user, or you need permission to view and write its data."

any thoughts?


回答1:


try to close the opened table first in access if you are editing them, and try to add "@" before the string to use your path. then try to use this connection string;

string connectionString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + yourDataBasePath + ";Persist Security Info=False;";


来源:https://stackoverflow.com/questions/15076417/reading-tables-using-oledbconnection

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!