Tom
Tom

Reputation: 13

C# OLEDB Database Connection Using Access 2016

I have been trying to get a database connection using OLEDB and Access 2016 unfortunately however I am continuously returned the error:

    System.Data.OleDb.OleDbException was unhandled
  ErrorCode=-2147467259
  HResult=-2147467259
  Message=Not a valid file name.
  Source=Microsoft Office Access Database Engine
  StackTrace:
       at System.Data.OleDb.OleDbConnectionInternal..ctor(OleDbConnectionString constr, OleDbConnection connection)
       at System.Data.OleDb.OleDbConnectionFactory.CreateConnection(DbConnectionOptions options, DbConnectionPoolKey poolKey, Object poolGroupProviderInfo, DbConnectionPool pool, DbConnection owningObject)
       at System.Data.ProviderBase.DbConnectionFactory.CreateConnection(DbConnectionOptions options, DbConnectionPoolKey poolKey, Object poolGroupProviderInfo, DbConnectionPool pool, DbConnection owningConnection, DbConnectionOptions userOptions)
       at System.Data.ProviderBase.DbConnectionFactory.CreateNonPooledConnection(DbConnection owningConnection, DbConnectionPoolGroup poolGroup, DbConnectionOptions userOptions)
       at System.Data.ProviderBase.DbConnectionFactory.TryGetConnection(DbConnection owningConnection, TaskCompletionSource`1 retry, DbConnectionOptions userOptions, DbConnectionInternal oldConnection, DbConnectionInternal& connection)
       at System.Data.ProviderBase.DbConnectionInternal.TryOpenConnectionInternal(DbConnection outerConnection, DbConnectionFactory connectionFactory, TaskCompletionSource`1 retry, DbConnectionOptions userOptions)
       at System.Data.ProviderBase.DbConnectionClosed.TryOpenConnection(DbConnection outerConnection, DbConnectionFactory connectionFactory, TaskCompletionSource`1 retry, DbConnectionOptions userOptions)
       at System.Data.ProviderBase.DbConnectionInternal.OpenConnection(DbConnection outerConnection, DbConnectionFactory connectionFactory)
       at System.Data.OleDb.OleDbConnection.Open()
       at Program_V1._0.Form1..ctor() in C:\Users\Tom\Documents\College\Computer_Science_Project _2017_Ver2.0\Program_Files\Program_V1.0\Program_V1.0\Form1.cs:line 24
       at Program_V1._0.Program.Main() in C:\Users\Tom\Documents\College\Computer_Science_Project _2017_Ver2.0\Program_Files\Program_V1.0\Program_V1.0\Program.cs:line 19
       at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
       at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
       at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
       at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
       at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
       at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
       at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
       at System.Threading.ThreadHelper.ThreadStart()
  InnerException: 

From this section of code:

    //Database Connection
    OleDbConnection connection = new OleDbConnection();
    connection.ConnectionString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=‪C:\Users\Tom\Documents\College\Computer_Science_Project _2017_Ver2.0\Program_Files\Program_V1.0\Program_V1.0\Databases\DecodeTimes.accdb;
    Persist Security Info=False;";
    connection.Open();
    label3.Text = " - Database Connected - ";
    connection.Close();

Try as I might I cannot see any issue with this even after reading about issues regarding compatibility, any help would be greatly appreciated.

Upvotes: 0

Views: 1538

Answers (1)

Gord Thompson
Gord Thompson

Reputation: 123484

When I copy your connection string and paste it into Notepad++ set for "ANSI" encoding I see

... Data Source=?C:\ ...

which tells me that your connection string contains a non-ANSI character. Further investigation shows that the mystery character (which appears as ? above) is actually the invisible Unicode character U+202A (LEFT-TO-RIGHT EMBEDDING). Deleting that invisible character from the connection string should resolve the issue.

Upvotes: 1

Related Questions