Reputation: 517
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.
Upvotes: 2
Views: 1649
Reputation: 517
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.
Upvotes: 1
Reputation: 601
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
Upvotes: 0
Reputation: 288
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=");
Upvotes: 1