VB.NET - 不支持关键字:provider

时间:2015-08-21 12:24:28

标签: vb.net

当我尝试连接到AcessDB时,出现错误

  

“不支持关键字:提供商”。

当我尝试更改提供程序时,我收到另一个错误。当我删除提供者标签时,我也会收到错误。

Dim Exists As Boolean = False
Dim ConnectionString As String = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\Name\Documents\Visual Studio 2010\Projects\datagrid\datagrid\pokus.accdb;Persist Security Info=False;"
Dim connection As New SqlConnection(ConnectionString)
Try
    connection.Open()
    Dim command As SqlCommand = connection.CreateCommand
    command.CommandText = "SELECT * FROM studenti"

    Dim reader As SqlDataReader = command.ExecuteReader
    If reader.HasRows Then
        Exists = True
    Else
        Exists = False
    End If
    reader.Close()
    command.Dispose()
Catch ex As Exception
    Console.Write(ex.Message)
Finally
    connection.Close()
End Try

2 个答案:

答案 0 :(得分:1)

SqlConnectionSqlCommand等是SQL Server个特定类。您无法使用它们连接到MS Access。

SqlConnection的文档非常清楚:

  

表示与 SQL Server 数据库的开放连接。

请考虑使用OldDbConnection及相关类。

答案 1 :(得分:0)

我总是为数据库连接编写包装类,我总是推荐它。

Dim sConnectionString As String = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\Name\Documents\Visual Studio 2010\Projects\datagrid\datagrid\pokus.accdb;Persist Security Info=False;"

Dim Conn As New OleDbConnection
Conn.ConnectionString = sConnectionString 
Conn.Open()

Dim sQuery As String = "SELECT * FROM studenti"
Dim da As New OleDbDataAdapter(sQuery, Conn)
Dim dt As New DataTable
da.Fill(dt)

Conn.Close()