简体   繁体   中英

Can't figure out OLE DB connection, C# to Access 2016

I am coding in VS Community 2015. I am on a new computer with Windows 10, just upgraded from 8.0. I have a stand-alone (no Office suite) Access 2016 just installed. It's all working fine.

I can't find squat that will give me a connection string for Access 2016. So I tried this code (in a dozen incarnations):

    public OleDbConnection TryOleDbConnection()
    {
        string ConnString =
            "Provider=Microsoft.ACE.OLEDB.15.0;" + 
            "Data Source=" + 
            "C:\\A A A A AutoBot4\\CALENDAR\\CALENDAR.addcb;" +
            "User Id=admin; Password=;";
        MessageBox.Show(ConnString);

        OleDbConnection OLE = new OleDbConnection();
        OLE.ConnectionString = ConnString;

        try
        {
            OLE.Open();
            MessageBox.Show("Opened");
        }
        catch (Exception ex)
        {
            MessageBox.Show("Failed");
        }
        finally
        {
        }
        return OLE;
    }

I can't get anything to work, and am not sure how to get better error information. I am on a non-networked computer and am the only user and administrator. I am also confused about User ID and Password, not understanding if they refer to system or database or something else. Any help here will be greatly appreciated! I feel sure that once I get a connection the rest will be easy.

You could use a function like this:

 public void FindProvider()
 {
      var reader = OleDbEnumerator.GetRootEnumerator();

      var list = new List<String>();
      while (reader.Read())
      {
           for (var i = 0; i < reader.FieldCount; i++)
           {
                if (reader.GetName(i) == "SOURCES_NAME")
                {
                     list.Add(reader.GetValue(i).ToString());
                }
           }
      }
      reader.Close();
      foreach (var provider in list)
      {
           if (provider.StartsWith("Microsoft.ACE.OLEDB"))
           {
                this.provider = provider.ToString();
           }
      }
 }

All it does is search through possible providers on your system and selects the one that matches Microsoft.ACE.OLEDB, but you can change this to however you would like. You also need to be aware that your application must be running in 64 bit mode if you have the 64 bit version of Office 2016, and 32 bit if your office version is 32 bit.

First of all, an advice to you :

Use ConnectionString Builder , or use App.config file to store your connectionstring.

Second : The database extension is " accdb " not " addcb "

And, finally " Yes ", the connectionstring of Microsoft Access Database 2016 is not or rarely out there. But if you ever googled " Microsoft Access Database Engine 2016 Redistributable " which I assume you did not because you have it installed already, right? isn't it? you would also find that it says (Installation instructions section) :

If you are an application developer using OLEDB, set the Provider argument of the ConnectionString property to “Microsoft.ACE.OLEDB.12.0” Which is incorrect indeed and discussion about this piece of misleading information is waiting an answer I was having the same issue, and my code to come over it, is, and as @heedfulcrayon previously mentioned:

Imports System.Data.OleDb
Public Class ThisClass
  Private ConnectionString As String
  Private CN As OleDbConnection = New OleDbConnection
  'This function Returns the Oledb Provider for your Project
  'I.e: if you have Office 2016 installed, it will return :
  'Microsoft.ACE.OLEDB.16.0
  Public Function FindProvider() As String
        Dim Provider As String = String.Empty
        Dim reader = OleDbEnumerator.GetRootEnumerator()
        Dim list = New List(Of String)
        While reader.Read()
            For i = 0 To reader.FieldCount - 1
                If reader.GetName(i) = "SOURCES_NAME" Then
                    list.Add(reader.GetValue(i).ToString())
                End If
            Next
        End While
        Return Nothing
        reader.Close()
        For Each provider In list
            If Provider.StartsWith("Microsoft.ACE.OLEDB") Then
                Provider = Provider.ToString()
                Return Provider
            Else
                Return Nothing
                Exit Function
            End If
        Next
    End Function
'This function is to validate connection to the database *.accdb
Public Function DBConnected(ByVal DBLocation As String, ByVal DBPass As String) As Boolean
ConnectionString =
("Provider=" & FindProvider() & ";Data Source=" & DBLocation & ";" _
& "Jet OLEDB:Database Password = '" & DBPass & "'; " _
& "Persist Security Info=False;")
  CN.ConnectionString = ConnectionString
  If CN.State = ConnectionState.Open Then CN.Close()
        Try
            CN.Open()
        Catch ex As OleDbException
            MsgBox("Error Database connection : " & ex.Message, MsgBoxStyle.Critical)
            Result = False
            Return Result
            Exit Function
        End Try
        Result = True
        Return Result
    End Function
    'Ofcourse this is not secure connection String, but just an example.
    'You should always use Configuration Files to manage Database Connections.
End Class

Now, remember you can just use Microsoft.ACE.OLEDB.16.0 , or you can use the whole script above, which if it worked, then it means that everything regarding your Access 2016 installation is fine.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM