27 February 2014

How To Connect To MS Access Database In Asp.Net And C#.Net

In this example i m using the northwind database that install (optionally) with MS Access database

First Drag a Grid view and Link Button control in your desing page

Use the following Namespaces in your .cs file

using System.Data;
using System,Data.oledb;

now write the following code in your Link Button click event

string conString =@"Provider=Microsoft.JET.OLEDB.4.0;"
+ @"data source=c:\data\Northwind.mdb";

// create an open the connection 
OleDbConnection conn = new OleDbConnection(conString);
conn.Open();

// create the DataSet
DataSet ds = new DataSet();

// create the adapter and fill the DataSet
OleDbDataAdapter adapter=new OleDbDataAdapter("Select * from Customers", conn);
adapter.Fill(ds);

// close the connection
conn.Close();
//Bind with grid view
GridView1.DataSource=ds;
GridView1.DataBind();

No comments:

Post a Comment