C# - Using Access Database - Add,Delete & Update
All tutorials created in C# to be posted in here.
1 post
Page 1 of 1
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):
button :2
button 3:
button 4:
full code :
Thanks
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.

this is my form :

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
this is how we add data into the database 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();
}
button :2
Code: Select all
this is how we update data 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();
button 3:
Code: Select all
this is how we delete data 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();
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
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.
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();
}
}
}
}
Thanks
Find my programs on Softpedia
1 post
Page 1 of 1
Copyright Information
Copyright © Codenstuff.com 2020 - 2023