C# - Using Access Database - Add,Delete & Update

All tutorials created in C# to be posted in here.
1 post Page 1 of 1
Contributors
User avatar
Shim
VIP - Donator
VIP - Donator
Posts: 882
Joined: Wed Dec 14, 2011 5:02 am

Hi,

First of all this is a fast tutorial, I think I'm already late.. watch this tutorial on how to link Access databse to your C# project https://www.youtube.com/watch?v=EoVsuJ_Exvs...

Now coming to the point, this is my database so you know the table name and column names.

Image

this is my form :

Image

this how we search, if you know the id you can enter it and click search then it will show name, address and telephone.

Button1 (click):
Code: Select all
  string sql;
            sql = string.Format("SELECT * FROM StDEtails WHERE ID ='" + textBox1.Text + "'");
            OleDbCommand cmd = new OleDbCommand(sql, con);
            con.Open();
            OleDbDataReader dr = cmd.ExecuteReader();

            if (dr.Read())
            {
                textBox2.Text = dr["Name"].ToString();
                textBox3.Text = dr["Address"].ToString();
                textBox4.Text = dr["Tele"].ToString();
            }
            else
            {
                MessageBox.Show("Invalid ID");
            }
            con.Close();
          }
this is how we add data into the database

button :2
Code: Select all
 String sql = "insert into StDEtails values('"+ textBox1.Text + "','" + textBox2.Text + "','" + textBox3.Text + "','" + textBox4.Text + "')";
            OleDbCommand cmd = new OleDbCommand(sql, con);
            con.Open();
            cmd.ExecuteNonQuery();
            MessageBox.Show("");
            con.Close();
this is how we update data

button 3:
Code: Select all
 String sql = "insert into StDEtails values('"+ textBox1.Text + "','" + textBox2.Text + "','" + textBox3.Text + "','" + textBox4.Text + "')";
            OleDbCommand cmd = new OleDbCommand(sql, con);
            con.Open();
            cmd.ExecuteNonQuery();
            MessageBox.Show("");
            con.Close();
this is how we delete data
button 4:
Code: Select all
 DialogResult r = MessageBox.Show("Are You Sure You Want To Delete This Record ?", "Delete", MessageBoxButtons.YesNo,MessageBoxIcon.Question);

            if (r == DialogResult.Yes)
            {
                String sql = "DELETE FROM StDEtails WHERE ID='" + textBox1.Text + "'";
                OleDbCommand cmd = new OleDbCommand(sql, con);
                con.Open();
                cmd.ExecuteNonQuery();
                MessageBox.Show("");
                con.Close();
                textBox1.Clear();
                textBox2.Clear();
                textBox3.Clear();
                textBox4.Clear();
            }
...

full code :
Code: Select all

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.OleDb;

namespace STuden
{
    public partial class Form2 : Form
    {
        OleDbConnection con = new OleDbConnection(@"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=|DataDirectory|\Student REgistry.mdb");
        public Form2()
        {

            InitializeComponent();
        }

        private void Form2_Load(object sender, EventArgs e)
        {

        }

        private void button1_Click(object sender, EventArgs e)
        {
             string sql;
            sql = string.Format("SELECT * FROM StDEtails WHERE ID ='" + textBox1.Text + "'");
            OleDbCommand cmd = new OleDbCommand(sql, con);
            con.Open();
            OleDbDataReader dr = cmd.ExecuteReader();

            if (dr.Read())
            {
                textBox2.Text = dr["Name"].ToString();
                textBox3.Text = dr["Address"].ToString();
                textBox4.Text = dr["Tele"].ToString();
            }
            else
            {
                MessageBox.Show("Invalid ID");
            }
            con.Close();
          }

        private void button2_Click(object sender, EventArgs e)
        {
      
           String sql = "insert into StDEtails values('"+ textBox1.Text + "','" + textBox2.Text + "','" + textBox3.Text + "','" + textBox4.Text + "')";
            OleDbCommand cmd = new OleDbCommand(sql, con);
            con.Open();
            cmd.ExecuteNonQuery();
            MessageBox.Show("");
            con.Close();
        }

        private void button3_Click(object sender, EventArgs e)
        {
            String sql = "update StDEtails set Name='" + textBox2.Text + "',Address='" + textBox3.Text + "', Tele = '" + textBox4.Text + "' where ID='" + textBox1.Text + "'";
         OleDbCommand cmd = new OleDbCommand(sql, con);
            con.Open();
            cmd.ExecuteNonQuery();
            MessageBox.Show("");
            con.Close();
        }

        private void button4_Click(object sender, EventArgs e)
        {
            DialogResult r = MessageBox.Show("Are You Sure You Want To Delete This Record ?", "Delete", MessageBoxButtons.YesNo,MessageBoxIcon.Question);

            if (r == DialogResult.Yes)
            {
                String sql = "DELETE FROM StDEtails WHERE ID='" + textBox1.Text + "'";
                OleDbCommand cmd = new OleDbCommand(sql, con);
                con.Open();
                cmd.ExecuteNonQuery();
                MessageBox.Show("");
                con.Close();
                textBox1.Clear();
                textBox2.Clear();
                textBox3.Clear();
                textBox4.Clear();
            }
        }
        }
    }

I made this simple system for my course, I am sorry I am not able to explain this clearly because it's too late and I have wrist pain so please.

Thanks
Find my programs on Softpedia
1 post Page 1 of 1
Return to “C-Sharp Tutorials”