通过 VB.NET 2010 连接访问 2013 数据库时出现无效文件名错误

Not a valid file name error while connecting to access 2013 database via VB.NET 2010

我必须进行干净的系统安装,但丢失了我正在制作的项目。然后我从头开始,但连接有困难。我收到错误“不是有效的文件名”。奇怪的是我能够检索组合框的数据。以下是异常详情:

Interception de System.Data.OleDb.OleDbException
  ErrorCode=-2147467259
  HResult=-2147467259
  Message=Not a valid file name.
  Source=Microsoft Office Access Database Engine
  StackTrace:
       à System.Data.OleDb.OleDbConnectionInternal..ctor(OleDbConnectionString constr, OleDbConnection connection)
       à System.Data.OleDb.OleDbConnectionFactory.CreateConnection(DbConnectionOptions options, DbConnectionPoolKey poolKey, Object poolGroupProviderInfo, DbConnectionPool pool, DbConnection owningObject)
       à System.Data.ProviderBase.DbConnectionFactory.CreateConnection(DbConnectionOptions options, DbConnectionPoolKey poolKey, Object poolGroupProviderInfo, DbConnectionPool pool, DbConnection owningConnection, DbConnectionOptions userOptions)
       à System.Data.ProviderBase.DbConnectionFactory.CreateNonPooledConnection(DbConnection owningConnection, DbConnectionPoolGroup poolGroup, DbConnectionOptions userOptions)
       à System.Data.ProviderBase.DbConnectionFactory.TryGetConnection(DbConnection owningConnection, TaskCompletionSource`1 retry, DbConnectionOptions userOptions, DbConnectionInternal oldConnection, DbConnectionInternal& connection)
       à System.Data.ProviderBase.DbConnectionInternal.TryOpenConnectionInternal(DbConnection outerConnection, DbConnectionFactory connectionFactory, TaskCompletionSource`1 retry, DbConnectionOptions userOptions)
       à System.Data.ProviderBase.DbConnectionClosed.TryOpenConnection(DbConnection outerConnection, DbConnectionFactory connectionFactory, TaskCompletionSource`1 retry, DbConnectionOptions userOptions)
       à System.Data.ProviderBase.DbConnectionInternal.OpenConnection(DbConnection outerConnection, DbConnectionFactory connectionFactory)
       à System.Data.OleDb.OleDbConnection.Open()
       à GestBanque.Securite.rechutil() dans F:\CODEBASE\HLVL\DotNet\GestBanque\GestBanque\Securite.vb:ligne 39
  InnerException: 

这是错误的代码:

Private Sub rechutil()
        Try
            Dim dr As OleDbDataReader
            Dim cmdselect As New OleDbCommand
            Dim sql As String
            Dim cn As String
            Dim sconnexion As OleDbConnection
            cn = "provider=Microsoft.ACE.OLEDB.12.0;Password=;User ID=Admin;Data Source=‪F:\GPI3\GestBanque.accdb"
            sconnexion = New OleDbConnection(cn)
            sconnexion.Open() ' The error is raised here
            cmdselect.Connection = sconnexion
            cmdselect.CommandType = CommandType.Text
            sql = "SELECT nom, motdepasse FROM UTILISATEUR WHERE nom=? AND motdepasse=?"
            cmdselect.CommandText = sql
            cmdselect.Parameters.Add("no", OleDbType.Char)
            cmdselect.Parameters.Add("mo", OleDbType.Char)
            cmdselect.Parameters("no").Value = cmbnom.Text
            cmdselect.Parameters("mo").Value = txtpass.Text
            dr = cmdselect.ExecuteReader
            If dr.HasRows Then
                Accueil.Show()
            Else
                MessageBox.Show("Désolé, Accès refusé, Mot de passe erronné", "Authentification", MessageBoxButtons.OK, MessageBoxIcon.Error)
                cmbnom.Text = ""
                txtpass.Text = ""
            End If
            sconnexion.Close()
        Catch ex As Exception
            MessageBox.Show(ex.Message, "Authentification", MessageBoxButtons.OK, MessageBoxIcon.Error)
        End Try
    End Sub

相反,这个程序运行得非常好:

Private Sub loadcombo()
        Try
            Dim dr As OleDbDataReader
            Dim cmdselect As New OleDbCommand
            Dim sql As String
            Dim cn As String
            Dim sconnexion As OleDbConnection
            cn = "provider=Microsoft.ACE.OLEDB.12.0;Password=;User ID=Admin;Data Source=F:\GPI3\GestBanque.accdb"
            sconnexion = New OleDbConnection(cn)
            sconnexion.Open()
            cmdselect.Connection = sconnexion
            cmdselect.CommandType = CommandType.Text
            sql = "SELECT nom FROM UTILISATEUR ORDER BY nom"
            cmdselect.CommandText = sql
            dr = cmdselect.ExecuteReader
            cmbnom.Items.Clear()
            While dr.Read
                cmbnom.Items.Add(dr.GetValue(0))
            End While
            sconnexion.Close()
        Catch ex As Exception
            MessageBox.Show(ex.Message, "Chargement des noms", MessageBoxButtons.OK, MessageBoxIcon.Error)
        End Try
    End Sub.

这是屏幕截图:Screen capt

我已经搜索了大约一个月,但在各种论坛上都没有找到解决方案。 提前致谢。

遇到此类奇怪的错误时,请检查您的字符串是否包含不可见的 Unicode 字符,这些字符可能是通过复制和粘贴引入的。例如,这行 VB.NET 代码看起来非常良性:

Dim spec As String = "‪‪W:\test.accdb"

但是如果我运行代码

Dim spec As String = "‪‪W:\test.accdb"
For i As Integer = 0 To 4
    Console.WriteLine(Hex(AscW(spec.Substring(i, 1))))
Next

我在控制台输出中看到了这个

202A
202A
57
3A
5C

它告诉我字符串的前两个字符是 invisible Unicode "left-to-right embedding" 字符 U+202A。尽管该字符串看起来像是一个有效的路径规范,但实际上并非如此。

已确认

已通过下载您的项目进行确认。 "Securite.vb" 的第 37 行在 "F:" 之前包含上面引用的不可见 Unicode 字符 (U+202A),但第 12 行不包含该字符。这就是为什么一个 Sub 失败而另一个没有的原因。