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); 
            } 
        } 
    } 

}

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.