通过VB.NET 2010连接到Access 2013数据库时,不是有效的文件名错误

时间:2016-06-27 09:04:50

标签: vb.net ms-access database-connection

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

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

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

1 个答案:

答案 0 :(得分:1)

当遇到像这样的奇怪错误时,请检查您的字符串是否包含可能通过复制和粘贴引入的不可见的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

它告诉我字符串的前两个字符是不可见 Unicode“从左到右嵌入”字符U+202A。尽管字符串看起来像一个有效的路径规范,但实际上并非如此。

确认

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