How to retrieve and display data from database (ms access) using c#.net



Step 1: Start the visual studio 2010 and open new project
design the form like

 
 
Step 2 : Create the database and add some fields like

           

Step 3 : Connect the access database form server explorer like
Server explorer->add connection->


Browse ->select the particular access database
Check test connection and press ok.
Step 4: Write the coding like
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.OleDb;

namespace exam1
{
    public partial class Form1 : Form
    {
       OleDbConnection vcon = new OleDbConnection (@"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Users\ANNAS\Documents\Visual Studio 2010\Projects\exam1\exam1\exa.mdb");
        public Form1()
        {
            InitializeComponent();
        }

private void Form1_Load(object sender, EventArgs e)
        {
            vcon.Open();
        }
//get the connection link from server explorer choose particular database properties for connection string
private void button1_Click(object sender, EventArgs e)
        {
            string vsql = "select * from show where firstname='"+textBox1 .Text +"'";
            OleDbCommand vcom = new OleDbCommand(vsql, vcon);
            DataSet vds = new DataSet();
            OleDbDataAdapter vda = new OleDbDataAdapter(vcom);
            vda.Fill(vds, "res");
            dataGridView1.DataSource = vds.Tables["res"];
            vda.Dispose();
            vcom.Dispose();
        }

private void button2_Click(object sender, EventArgs e)
        {
            string vsql = "select * from show";
            OleDbCommand vcom = new OleDbCommand(vsql, vcon);
            DataSet vds = new DataSet();
            OleDbDataAdapter vda = new OleDbDataAdapter(vcom);
            vda.Fill(vds, "res");
            dataGridView1.DataSource = vds.Tables["res"];
            vda.Dispose();
            vcom.Dispose();
        }

    }
}

Step 5: Execute the project like



Step 6: Stop the Program execution.

Post a Comment

0 Comments