Question

Using the code below:

OleDbConnection con = new OleDbConnection("Provider = Microsoft.Jet.OLEDB.4.0; Data Source = C:\\MyFolder\\MyFile.mdb");
OleDbCommand cmd = new OleDbCommand();
cmd.Connection = con;
cmd.CommandText = sql;
con.Open();
recordsAffected = cmd.ExecuteNonQuery();
con.Close();

I get the following error:

An unhandled exception of type 'System.Data.OleDb.OleDbException' occurred in System.Data.dll

Additional information: Could not find file 'C:\MyFolder\MyFile.mdb'.

This is in a WinForms application (C#). The file exists in that folder. I am running the application as an administrative user.

FYI, when I attempt to use a System.IO.File.Exists(@"C:\MyFolder\MyFile.mdb"), it returns false, stating that the file does not exist. I can see the mdb in this very folder, though.

Was it helpful?

Solution 2

The problem was occurring because of my SQL syntax that was hitting the Access DB.

If you specify a dot syntax (IE: SELECT * FROM [schemaname].[tablename]), the OLEDB driver will assume that the [schemaname] is the MDB file name, and will replace the file name/location specified in the connection string file with that.

My schema name happened to be the exact same name as the file name, so it was trying to find the MDB file in the wrong location.

All I had to do to fix the issue was to change my query from SELECT * FROM [schemaname].[tablename] to SELECT * FROM [tablename] and everything worked just fine.

OTHER TIPS

This connection string should work:

OleDbConnection con = new OleDbConnection(@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\MyFolder\MyFile.mdb;Jet OLEDB:Database Password=");

Might be it is looking for the file from ur application location for an example /bin/Debug debug your connection object to see Datasource path it will giv u a hint on the issue

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top