Hi,

I'm doing the following code to connect to Access Db. The db1.mdb is placed in c:\db1.mdb. It giving me error "couldn't find file"

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System; 
using System.Data.OleDb; 

namespace CustomerDB
{

    class OleDbTest{ 

        public static void Main() 
        { 
            //create the database connection 
            OleDbConnection aConnection = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=c:/db1.mdb"); 

            //create the command object and store the sql query 
            OleDbCommand aCommand = new OleDbCommand("select * from customer_test", aConnection);       

            try 
            { 
                aConnection.Open(); 

                //create the datareader object to connect to table 
                OleDbDataReader aReader = aCommand.ExecuteReader(); 
                Console.WriteLine("This is the returned data from customer_test table"); 

                //Iterate throuth the database 
                while(aReader.Read()) 
                { 
                    Console.WriteLine(aReader.GetInt32(0).ToString()); 
                } 

                //close the reader 
                aReader.Close(); 

                //close the connection Its important. 
                aConnection.Close();

                Console.ReadLine();
            } 

            //Some usual exception handling 
            catch(OleDbException e) 
            { 
                Console.WriteLine("Error: {0}", e.Errors[0].Message); 
            } 
        } 
    } 

}

Dani AI

Generated

Summary: 's suggestion fixed the immediate problem and confirmed success. For others who see a similar "couldn't find file" error when opening an Access file from C#, the root cause is often not the SQL or reader loop but the runtime environment or path visibility. Below are concise, practical diagnostics and fixes that complement the replies in this thread.

Check the file path and existence at runtime before opening a connection. Build paths relative to the running app and verify the file is where the process expects it:

using System.IO;

string dbFile = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "db1.mdb");
if (!File.Exists(dbFile))
{
    Console.WriteLine("Database file not found: " + dbFile);
    return;
}

If the file is missing, the deployment or working directory is wrong. If the file exists but the connection still fails, confirm the process user has read access, try opening the file directly in Access to rule out corruption, and verify no other process holds an exclusive lock.

Environment and deployment checks to avoid false "file not found" symptoms: ensure the application runs with the same bitness (x86/x64) that the installed data engine supports (mismatched bitness frequently causes provider/driver errors), avoid hard-coded C:\ paths in deployed apps (use config settings or relative paths), and prefer UNC paths for network locations. For multi-user or production scenarios consider moving to a server-grade DB engine to avoid file-locking and concurrency problems. These steps cover additional failure modes beyond the connection-string fix already applied in this thread.

Recommended Answers

All 4 Replies

Paths are normally giving with the \ character not /. Also which version of Access created the database?

Access 2007. The error is the same even with \

2007 uses a different provider:

Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\myFolder\myAccess2007file.accdb;Persist Security Info=False;

Note that the extension should be accdb, not mdb

Thanks, it worked.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.