找到正确的 OleDb 连接字符串 VB.Net

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14699111/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 12:11:51  来源:igfitidea点击:

Finding the correct OleDb connection string VB.Net

vb.netconnection-stringoledboledbconnection

提问by Hank

I am creating a 32bit app on an old XP machine, this app will run on Win7 or later as well.

我正在旧 XP 机器上创建一个 32 位应用程序,该应用程序也将在 Win7 或更高版本上运行。

so I am trying to test for what connection string will work. Such as:

所以我试图测试什么连接字符串可以工作。如:

Private Function test_ace_or_jet(ByVal mdb_path As String) As String
        Dim connString As String
        Dim dbMaintPort As OleDb.OleDbConnection

        connString = ""

        Try
            connString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & mdb_path

            dbMaintPort = New OleDb.OleDbConnection(connString)
            dbMaintPort.Open()

            dbMaintPort.Close()

            MsgBox("1 was found")
        Catch ex As Exception
            MsgBox("Could not open 1" & vbCrLf & vbCrLf & ex.Message)
            Try
                connString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & mdb_path
                dbMaintPort = New OleDb.OleDbConnection(connString)
                dbMaintPort.Open()
                dbMaintPort.Close()

                MsgBox("2 was found")
            Catch ex2 As Exception
                connString = ""
                MsgBox("Could not open 2" & vbCrLf & vbCrLf & ex2.Message)
            End Try
        End Try
        test_ace_or_jet = connString
    End Function

However the issue that I've found is that on the XP machine, the first connection string does not fail. It does not have ACE installed at all.

然而,我发现的问题是在 XP 机器上,第一个连接字符串不会失败。它根本没有安装ACE。

How can I test whether to use one or the other? Am I able to get the above function to give me the correct connection string?

我如何测试是使用一种还是另一种?我能否获得上述功能以提供正确的连接字符串?

采纳答案by DRapp

if you keep doing your try/catch until you find something, you might be nesting yourself quite deep... You may be better (while testing), doing a loop and have an array of connection strings something like

如果您继续尝试/捕获直到找到某些东西,那么您可能会将自己嵌套得很深……您可能会更好(在测试时),执行循环并拥有一系列连接字符串,例如

Private Function test_ace_or_jet(ByVal mdb_path As String) As String
   Dim connString As String
   Dim dbMaintPort As OleDb.OleDbConnection
   Dim ValidConnection As Boolean
   Dim finalConnection As String
   Dim connStrings(2) As String
   connStrings(0) = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & mdb_path
   connStrings(1) = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & mdb_path
   connStrings(2) = "AnyOtherProviderTestConnectionStrings..."

   ValidConnection = False
   For value As Integer = 0 To 2
      Try
         dbMaintPort = New OleDb.OleDbConnection(connStrings(value))
         dbMaintPort.Open()
         dbMaintPort.Close()

         MsgBox("Success: " & connStrings(value))
         ValidConnection = True
         finalConnection = connStrings(value)
      Catch 
      End Try

      if ValidConnection
         exit
      endif 

   Next

   ; continue with whatever else you want to do with test result and final string

End Function

Also, have you had a look at ConnectionStrings.com? They might give you more options, and connection samples to test different providers and such.

另外,您是否看过ConnectionStrings.com?他们可能会为您提供更多选项和连接示例以测试不同的提供程序等。