You are on page 1of 2

using System.Data.

OleDb;

public partial class Form1 : Form


{
OleDbConnection vcon = new
OleDbConnection(@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=D:\db.accdb");
public Form1()
{
InitializeComponent();
}

private void button1_Click(object sender, EventArgs e) //View


{
string s1 = "select * from [t1]";
OleDbCommand vcomm = new OleDbCommand(s1, vcon);
DataSet vds = new DataSet();
OleDbDataAdapter vda = new OleDbDataAdapter(vcomm);
vda.Fill(vds, "res");
dataGridView1.DataSource = vds.Tables["res"];
vda.Dispose();
vcomm.Dispose();

private void Form1_Load(object sender, EventArgs e)


{
vcon.Open();
}

private void Add_Click(object sender, EventArgs e) // Insert


{
string vsql = string.Format("insert into [t1] values('{0}','{1}')",
textBox1.Text, textBox2.Text);
OleDbCommand vcomm = new OleDbCommand(vsql, vcon);
vcomm.ExecuteNonQuery();
MessageBox.Show("Added successful");
vcomm.Dispose();

private void button2_Click(object sender, EventArgs e) //Search


{
string s2 = string.Format("select * from [t1] where [no]='{0}'",
textBox3.Text);
//string s2 = "select * from Order where Roll_No='1010'";
OleDbCommand vcom = new OleDbCommand(s2, vcon);
DataSet vd = new DataSet();
OleDbDataAdapter vdap = new OleDbDataAdapter(vcom);
vdap.Fill(vd, "res");
dataGridView2.DataSource = vd.Tables["res"];
vdap.Dispose();
vcom.Dispose();

}
}
} Database Name:db ----- Table Name:t1

You might also like