Database Connectivity C Sharp to Access




Step 1:
using System.Data.OleDb;
Step 2:
public partial class Form1 : Form
    {
        private OleDbConnection conn;

Step 3:
InitializeComponent();
            conn = new OleDbConnection(@"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=F:\Lecture-notes\c-srp\db1.mdb");

save button code:
OleDbCommand cmd=new OleDbCommand();
            cmd.CommandType=CommandType.Text;
            cmd.CommandText = "insert into table1(name,fname,id,sem)values('" + textBox1.Text + "','" + textBox2.Text + "','" + textBox3.Text + "','" + textBox4.Text + "')";
            cmd.Connection = conn;
            conn.Open();
            cmd.ExecuteNonQuery();
            conn.Close();
==============================
del button code:
OleDbCommand cmd = new OleDbCommand();
            cmd.CommandType = CommandType.Text;
            cmd.CommandText = "delete from table1 where(id=" + textBox3.Text + ")";
            cmd.Connection = conn;
            conn.Open();
            cmd.ExecuteNonQuery();
            conn.Close();
=================================

update button code:

OleDbCommand cmd = new OleDbCommand();
            cmd.CommandType = CommandType.Text;
            cmd.CommandText = "update table1 set name='" + textBox1.Text + "',fname='" + textBox2.Text + "'where(id=" + textBox3.Text + ")";
            cmd.Connection = conn;
            conn.Open();
            cmd.ExecuteNonQuery();
            conn.Close();
new button code:
textBox1.Text = "";
            textBox2.Text = "";
            textBox3.Text = "";
            textBox4.Text = "";
search button code:
private void search_Click(object sender, EventArgs e)
        {
            if (radioButton1.Checked)
          
              this.dataGridView1.DataSource=this.database1DataSet.Table1.Select("rollno ='"+textBox5.Text+"'");
            else if(radioButton2.Checked)
                this.dataGridView1.DataSource=this.database1DataSet.Table1.Select("sname ='"+textBox5.Text+"'");

        }

        private void cancel_Click(object sender, EventArgs e)
        {
            this.dataGridView1.DataSource = this.database1DataSet.Table1;
        }

Comments