Wednesday, April 23, 2008

DATABASE OPERATION

Now ,for this time only ,i will tell you about a c# application working with MS Access instead of SQL server.

For that you must call the namespace "system.data.oledb" first.
Then you can easly connect your application with MS access.

Keep this script with you.Send me your reply on this


namespace access
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

private void Form1_Load(object sender, EventArgs e)
{
OleDbConnection cnn = new OleDbConnection(@"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=D:\data1.mdb");
cnn.Open();
OleDbDataAdapter ad = new OleDbDataAdapter("select *from employee",cnn);
cnn.Close();
DataTable dt = new DataTable();
ad.Fill(dt);
dataGridView1.DataSource = dt;



}


Here you can see , I am calling the namespace and making the string which carries the path to our database.As always then create a connection,open it.Make an instance of adapter , then merge adapter's source to a datatable .Atlast store that value to an instane of Datagridview .There you are ,you'll now have your data on your page.


Next time same project will be in SQL SERVER.
Feel free to ask doubts.

No comments: