Connecting C# application with MS Access database

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Aashish Upadhyay
    New Member
    • Aug 2008
    • 4

    Connecting C# application with MS Access database

    HI I have created a connection between C# application and MS ACCESS databse but when I am compiling it the error message ia displayed that file db1.mdb not found, please help me to figure out the solution?

    This is my code:

    Code:
    using System;
    using System.Data.OleDb;
    
    class OleDbTest{
    
    public static void Main()
    {
    OleDbConnection aConnection = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\\db1.mdb");
    
    
    OleDbCommand aCommand = new OleDbCommand("select * from emp_test", aConnection);
    try
    {
    aConnection.Open();
    
    OleDbDataReader aReader = aCommand.ExecuteReader();
    Console.WriteLine("This is the returned data from emp_test table");
    
    
    while(aReader.Read())
    {
    Console.WriteLine(aReader.GetInt32(0).ToString());
    }
    
    
    aReader.Close();
    
    
    aConnection.Close();
    }
    
    
    catch(OleDbException e)
    {
    Console.WriteLine("Error: {0}", e.Errors[0].Message);
    }
    }
    }
    Last edited by kenobewan; Aug 24 '08, 12:27 PM. Reason: Please use code tags
  • kenobewan
    Recognized Expert Specialist
    • Dec 2006
    • 4871

    #2
    Here is a reference that may help:
    This is a compiled connection strings reference list on how to connect to Access 2007.

    Comment

    • NitinSawant
      Contributor
      • Oct 2007
      • 271

      #3
      did u wrote the correct connectionstrin g


      Code:
      Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\db1.mdb;Persist Security Info=False

      Comment

      • kenobewan
        Recognized Expert Specialist
        • Dec 2006
        • 4871

        #4
        Originally posted by Nitin646
        did u wrote the correct connectionstrin g


        Code:
        Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\db1.mdb;Persist Security Info=False
        The OP is writing C# and I believe trying to avoid the single \ error...

        Comment

        • Curtis Rutland
          Recognized Expert Specialist
          • Apr 2008
          • 3264

          #5
          Originally posted by kenobewan
          The OP is writing C# and I believe trying to avoid the single \ error...
          Right.

          But I think since the db's extension is .mdb, OP is using Acces 2003 or lower, so:
          404 - Page Not Found. Shown when a URL cannot be mapped to any kind of resource.

          might be a better place to look.

          Access 07 uses ACE instead of JET, so it probably wont work for older versions.

          Comment

          • Aashish Upadhyay
            New Member
            • Aug 2008
            • 4

            #6
            Originally posted by insertAlias
            Right.

            But I think since the db's extension is .mdb, OP is using Acces 2003 or lower, so:
            404 - Page Not Found. Shown when a URL cannot be mapped to any kind of resource.

            might be a better place to look.

            Access 07 uses ACE instead of JET, so it probably wont work for older versions.

            thanks to All for reply, I got the solution

            Comment

            • Curtis Rutland
              Recognized Expert Specialist
              • Apr 2008
              • 3264

              #7
              Originally posted by Aashish Upadhyay
              thanks to All for reply, I got the solution
              Please post your solution so other people might benefit.

              Comment

              • Aashish Upadhyay
                New Member
                • Aug 2008
                • 4

                #8
                Originally posted by insertAlias
                Please post your solution so other people might benefit.

                as such there is no problem with the code, the only thing was that I was not allowed to save the database directly on C drive

                Comment

                Working...