问题
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