简体   繁体   English

无法弄清楚 OLE DB 连接,C# 到 Access 2016

[英]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.我正在 VS Community 2015 中编码。我在一台装有 Windows 10 的新计算机上,刚刚从 8.0 升级。 I have a stand-alone (no Office suite) Access 2016 just installed.我刚刚安装了一个独立的(没有 Office 套件)Access 2016。 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):我找不到可以为我提供 Access 2016 连接字符串的蹲坐。所以我尝试了这个代码(在十几种化身中):

    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.我也对用户 ID 和密码感到困惑,不明白它们是指系统还是数据库或其他东西。 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.它所做的只是在您的系统上搜索可能的提供程序并选择与 Microsoft.ACE.OLEDB 匹配的提供程序,但您可以将其更改为您想要的任何内容。 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.您还需要注意,如果您拥有 64 位版本的 Office 2016,您的应用程序必须在 64 位模式下运行,如果您的 Office 版本是 32 位,则您的应用程序必须在 32 位模式下运行。

First of all, an advice to you :首先给你一个忠告

Use ConnectionString Builder , or use App.config file to store your connectionstring.使用ConnectionString Builder ,或使用App.config文件来存储您的连接字符串

Second : The database extension is " accdb " not " addcb "第二:数据库扩展名是“ accdb ”而不是“ addcb

And, finally " Yes ", the connectionstring of Microsoft Access Database 2016 is not or rarely out there.最后,“”,Microsoft Access Database 2016 的连接字符串不存在或很少存在。 But if you ever googled " Microsoft Access Database Engine 2016 Redistributable " which I assume you did not because you have it installed already, right?但是,如果您曾经在 google 上搜索过“ Microsoft Access Database Engine 2016 Redistributable ”,我认为您没有搜索过,因为您已经安装了它,对吧? 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:如果您是使用 OLEDB 的应用程序开发人员,请将 ConnectionString 属性的 Provider 参数设置为“Microsoft.ACE.OLEDB.12.0” 这确实不正确,关于这条误导性信息的讨论正在等待答案我遇到了同样的问题,和我的代码来解决它,是,正如@heedfulcrayon 之前提到的:

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.现在,请记住您可以只使用Microsoft.ACE.OLEDB.16.0 ,或者您可以使用上面的整个脚本,如果它有效,那么这意味着与您的 Access 2016 安装有关的一切都很好。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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