C# program that interfaces with MS Access: Open an existing Access database file

1. Open a new Visual C# .NET windows application. Name the project OpentDatabase.

2. Design a form. Add a Textbox and a Button

3. Set Name property of Textbox to “txtdatabaseName”.

4. Set Name property of Button to “btnOpen” and text property to “Open Database”.

5. In the code file add namespace: using System.Data.OleDb;

6. Double click on the Button and pest the following code into the “btnOpen_Click” event

string strAccessConn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + txtDatabasePath.Text ;

myAccessConn = null;
                myAccessConn = new OleDbConnection(strAccessConn);  // create a
connection object
                myAccessConn.Open(); // open the connection
                MessageBox.Show("Database connection is successfully Opened");
(Exception ex)
                MessageBox.Show("Error: Failed to create a database connection.
\n{0}", ex.Message);
(myAccessConn.State == ConnectionState.Open)  //check the
connections status.
                myAccessConn.Close();  // close the
                MessageBox.Show("Database is successfully Closed");

7. Press F5 to build and run the project.

8. Put your existing database name with location like c:\MyAccessDB.mdb in the text box and then press the “Open Database” Button. Your access database connection will be opened.


anatali 6 years ago

Try EffiProz Database http://www.EffiProz.com,

EffiProz is a database written entirely in C#. comprehensive SQL support including Stored Procedures, Triggers and Functions. Ideal for embed in .Net applications. Support Silverlight 3 and .Net compact framework as well

Include Visual Studio ad-in, ADO.Net provider, Entity Framework provider, etc.

sruth 4 years ago

thank you..this is useful for me..

    Sign in or sign up and post using a HubPages Network account.

    0 of 8192 characters used
    Post Comment

    No HTML is allowed in comments, but URLs will be hyperlinked. Comments are not for promoting your articles or other sites.

    Click to Rate This Article