Connecting with MS Access 2007 database from C# Windows application

ttarakarajesh-8732
Posted by in C# category on for Beginner level | Points: 250 | Views : 57227 red flag

This article explains to us , how to connect with MS Access 2007 database from C# Windows application.
 

Introduction

First of all I would like to say thanks to all the readers. In this article, I am going to show you how to connect with MS Access 2007 database from C#.NET windows application.  Design our window as follows.

Form Design




Code


private void btnGetData_Click(object sender, EventArgs e)
{
string
conStr = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C\TestTable.accdb; Persist Security Info=False;";


OleDbConnection
con = new OleDbConnection(conStr);

try
{

con.Open();

OleDbCommand
cmd = new OleDbCommand("Select Sname,Sal from Tarak where ID="+txtSNO.Text, con);

OleDbDataReader
dr = cmd.ExecuteReader();

 

if
(dr.Read())

{

 
txtName.Text = dr["SName"].ToString();

 
txtSal.Text ="$"+ dr["sal"].ToString();

}

}

catch
(Exception ex)

{

lblMessage.Visible = true;

lblMessage.Text = ex.Message;

}

}


Conclusion


I hope this article helps for connecting with MS Access Database.

Happy Programming!!!!


Thanks & Regards,
Tarak

Page copy protected against web site content infringement by Copyscape

About the Author

ttarakarajesh-8732
Full Name: Taraka Rajesh Talagadadeevi
Member Level: Starter
Member Status: Member
Member Since: 1/28/2011 4:38:05 AM
Country: India



Login to vote for this post.

Comments or Responses

Login to post response

Comment using Facebook(Author doesn't get notification)