HI WELCOME TO KANSIRIS

Connecting with MS Access 2007 database from C# Windows application

Leave a Comment

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!!!!

0 comments:

Post a Comment

Note: only a member of this blog may post a comment.