使用OleDBConnection读表

时间:2013-02-25 21:00:40

标签: vb.net oledb oledbcommand

首先,我是VB新手,这是我第一个使用OleDBConnection的项目。

好的,所以我正在尝试使用oleDbConnection(我假设)最简单的事情。我只想从Access数据库中的表中读取数据,并将该信息显示在winForm中的dropbox(或任何内容)中。

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

尝试运行“taxconn.Open()”函数时会出现错误。

错误显示“Microsoft Access数据库引擎无法打开或写入文件'\ atrts10 \ F:\ Applications \ ATRTaxCert \ Development \ mtaylor \ TaxCert_be_test.accdb'。它已由其他用户独占打开,或者您需要获得查看和写入数据的权限。“

有什么想法吗?

1 个答案:

答案 0 :(得分:1)

如果您正在编辑它们,请尝试先关闭已打开的表,然后尝试在字符串前添加“@”以使用您的路径。 然后尝试使用此连接字符串;

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