Failing to open OleDbConnection to Jet database after setting TMP environment variable in the process

0

Earlier in the process, I set the TMP environment variable to a sibling folder of where the Jet database is. I use this code:

Environment.SetEnvironmentVariable("TMP", "<sibling to Jet database>");

When I call OleDbConnection.Open(), I get the error.
Here is the stacktrace:

System.Data.OleDb.OleDbException (0x80004005): Unspecified error
   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()  

If I set the environment variable in the shell before running the process instead of setting it in code in the process, I don't get the error.

set TMP=<sibling to Jet database>  

Here is my connection string:

Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\\<sibling to TMP>\\database.mdb;

I tried to create a sample console application, but it does not exhibit the same behavior. There it works. The application where I see the error is a WinForms application with target .NET Framework version set to .NET 4.6.1.

The folder designated in the value of the TMP environment variable does exist and permissions are set to full control to everyone.

Even if I set TMP to the same folder in the shell as in code in the process, I still get the error. If I set some other environment variable in code in the process, I do not get the error.

Update:
If I set the TMP environment variable at the user level, I don't see the error:

Environment.SetEnvironmentVariable("TMP", "<sibling to Jet database>", EnvironmentVariableTarget.User);  

Unfortunately that won't work for me as I only want the TMP environment variable to be changed in the scope of the process and not affect other process on the machine.

c#
environment-variables
oledbconnection
asked on Stack Overflow Sep 4, 2020 by 4thex • edited Sep 4, 2020 by 4thex

1 Answer

0

This is the error you will get if the TMP environment variable is set to a folder that does not exist.

answered on Stack Overflow Sep 10, 2020 by 4thex

User contributions licensed under CC BY-SA 3.0