The following source code performs CRUD operation using C# programming language. In addition, search function also added in this source code.
This code used for my future reference. I hope it could be helpful also. Thank you
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.OleDb;
namespace CRUD_Application_CSharp
{
public partial class Form1 : Form
{
String connectionString;
OleDbConnection conn;
OleDbDataAdapter da;
OleDbCommand cmd;
//DataSet ds;
string sql;
public int x;
public Form1()
{
InitializeComponent();
}
public void dbConnection()
{
connectionString = @"Provider=Microsoft.JET.OLEDB.4.0;Data Source=C:\\Users\Juno\Documents\dbInformation.mdb";
conn = new OleDbConnection(connectionString);
conn.Open();
//MessageBox.Show("Connection Open !");
//conn.Close();
}
private void btnReadDatabase_Click(object sender, EventArgs e)
{
retrieveALL();
}
private void retrieveALL(){
dbConnection();
sql = "SELECT * FROM tblProfile";
da = new OleDbDataAdapter(sql, conn);
DataTable dt = new DataTable(); //ds = new DataSet();
da.Fill(dt); //da.Fill(ds, "dbInformation");
dataGridView1.DataSource = dt; //dataGridView1.DataSource = ds.Tables[0];
conn.Close();
}
private void btnSave_Click(object sender, EventArgs e)
{
dbConnection();
sql = "INSERT INTO tblProfile (Firstname,Lastname,Gender) VALUES ('" + txtFname.Text + "','" + txtLname.Text + "','" + cbGender.Text + "')";
cmd = new OleDbCommand(sql, conn);
cmd.ExecuteNonQuery();
MessageBox.Show("Successfully Added to your Database", "New Record Added", MessageBoxButtons.OK, MessageBoxIcon.Information);
conn.Close();
//btnReadDatabase_Click(sender, e);
retrieveALL();
}
private void dataGridView1_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
{
x = dataGridView1.Rows.IndexOf(dataGridView1.CurrentRow);
txtFname.Text = dataGridView1.Rows[x].Cells[1].Value.ToString();
txtLname.Text = dataGridView1.Rows[x].Cells[2].Value.ToString();
cbGender.Text = dataGridView1.Rows[x].Cells[3].Value.ToString();
MessageBox.Show(dataGridView1.Rows[x].Cells[0].Value.ToString());
}
private void btnUpdate_Click(object sender, EventArgs e)
{
dbConnection();
int indexID;
x = dataGridView1.Rows.IndexOf(dataGridView1.CurrentRow);
indexID = Convert.ToInt32((dataGridView1.Rows[x].Cells[0].Value.ToString()));
sql = "UPDATE tblProfile SET Firstname= '" + txtFname.Text + "',Lastname='" + txtLname.Text +
"',Gender='" + cbGender.Text + "' WHERE ID=" + indexID + "";
cmd = new OleDbCommand(sql, conn);
cmd.ExecuteNonQuery();
MessageBox.Show("Successfully Updated...", "Update");
conn.Close();
//btnReadDatabase_Click(sender, e);
retrieveALL();
}
private void btnDelete_Click(object sender, EventArgs e)
{
dbConnection();
int indexID;
x = dataGridView1.Rows.IndexOf(dataGridView1.CurrentRow);
indexID = Convert.ToInt32((dataGridView1.Rows[x].Cells[0].Value.ToString()));
sql = "DELETE FROM tblProfile WHERE ID=" + indexID + "";
cmd = new OleDbCommand(sql, conn);
cmd.ExecuteNonQuery();
MessageBox.Show("Successfully Deleted...", "Delete");
conn.Close();
retrieveALL();
}
private void btnSearch_Click(object sender, EventArgs e)
{
dbConnection();
sql = "SELECT * FROM tblProfile WHERE Firstname='" + txtSearch.Text+ "' OR Lastname='" + txtSearch.Text +"'";
da = new OleDbDataAdapter(sql, conn);
DataTable dt = new DataTable(); //ds = new DataSet();
da.Fill(dt); //da.Fill(ds, "dbInformation");
dataGridView1.DataSource = dt; //dataGridView1.DataSource = ds.Tables[0];
conn.Close();
}
private void Form1_Load(object sender, EventArgs e)
{
dataGridView2.Columns.Add("Firstname","fname");
dataGridView2.Columns.Add("Lastname","lname");
dataGridView2.Columns.Add("Gender","gender");
retrieveALL();
}
private void btnClear_Click(object sender, EventArgs e)
{
txtFname.Clear();
txtLname.Clear();
txtSearch.Clear();
cbGender.Text = null;
}
private void button1_Click(object sender, EventArgs e)
{
for (int i = 0; i < dataGridView2.Rows.Count; i++)
{
sql = @"INSERT INTO tblProfile (Firstname,Lastname,Gender) VALUES ('"
+ dataGridView2.Rows[i].Cells["Firstname"].Value + "',' "
+ dataGridView2.Rows[i].Cells["Lastname"].Value + "', '"
+ dataGridView2.Rows[i].Cells["Gender"].Value + "')";
dbConnection();
cmd = new OleDbCommand(sql, conn);
cmd.ExecuteNonQuery();
MessageBox.Show("Successfully Added to your Database", "New Record Added", MessageBoxButtons.OK, MessageBoxIcon.Information);
conn.Close();
//btnReadDatabase_Click(sender, e);
retrieveALL();
}
}
}
}
This code used for my future reference. I hope it could be helpful also. Thank you
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.OleDb;
namespace CRUD_Application_CSharp
{
public partial class Form1 : Form
{
String connectionString;
OleDbConnection conn;
OleDbDataAdapter da;
OleDbCommand cmd;
//DataSet ds;
string sql;
public int x;
public Form1()
{
InitializeComponent();
}
public void dbConnection()
{
connectionString = @"Provider=Microsoft.JET.OLEDB.4.0;Data Source=C:\\Users\Juno\Documents\dbInformation.mdb";
conn = new OleDbConnection(connectionString);
conn.Open();
//MessageBox.Show("Connection Open !");
//conn.Close();
}
private void btnReadDatabase_Click(object sender, EventArgs e)
{
retrieveALL();
}
private void retrieveALL(){
dbConnection();
sql = "SELECT * FROM tblProfile";
da = new OleDbDataAdapter(sql, conn);
DataTable dt = new DataTable(); //ds = new DataSet();
da.Fill(dt); //da.Fill(ds, "dbInformation");
dataGridView1.DataSource = dt; //dataGridView1.DataSource = ds.Tables[0];
conn.Close();
}
private void btnSave_Click(object sender, EventArgs e)
{
dbConnection();
sql = "INSERT INTO tblProfile (Firstname,Lastname,Gender) VALUES ('" + txtFname.Text + "','" + txtLname.Text + "','" + cbGender.Text + "')";
cmd = new OleDbCommand(sql, conn);
cmd.ExecuteNonQuery();
MessageBox.Show("Successfully Added to your Database", "New Record Added", MessageBoxButtons.OK, MessageBoxIcon.Information);
conn.Close();
//btnReadDatabase_Click(sender, e);
retrieveALL();
}
private void dataGridView1_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
{
x = dataGridView1.Rows.IndexOf(dataGridView1.CurrentRow);
txtFname.Text = dataGridView1.Rows[x].Cells[1].Value.ToString();
txtLname.Text = dataGridView1.Rows[x].Cells[2].Value.ToString();
cbGender.Text = dataGridView1.Rows[x].Cells[3].Value.ToString();
MessageBox.Show(dataGridView1.Rows[x].Cells[0].Value.ToString());
}
private void btnUpdate_Click(object sender, EventArgs e)
{
dbConnection();
int indexID;
x = dataGridView1.Rows.IndexOf(dataGridView1.CurrentRow);
indexID = Convert.ToInt32((dataGridView1.Rows[x].Cells[0].Value.ToString()));
sql = "UPDATE tblProfile SET Firstname= '" + txtFname.Text + "',Lastname='" + txtLname.Text +
"',Gender='" + cbGender.Text + "' WHERE ID=" + indexID + "";
cmd = new OleDbCommand(sql, conn);
cmd.ExecuteNonQuery();
MessageBox.Show("Successfully Updated...", "Update");
conn.Close();
//btnReadDatabase_Click(sender, e);
retrieveALL();
}
private void btnDelete_Click(object sender, EventArgs e)
{
dbConnection();
int indexID;
x = dataGridView1.Rows.IndexOf(dataGridView1.CurrentRow);
indexID = Convert.ToInt32((dataGridView1.Rows[x].Cells[0].Value.ToString()));
sql = "DELETE FROM tblProfile WHERE ID=" + indexID + "";
cmd = new OleDbCommand(sql, conn);
cmd.ExecuteNonQuery();
MessageBox.Show("Successfully Deleted...", "Delete");
conn.Close();
retrieveALL();
}
private void btnSearch_Click(object sender, EventArgs e)
{
dbConnection();
sql = "SELECT * FROM tblProfile WHERE Firstname='" + txtSearch.Text+ "' OR Lastname='" + txtSearch.Text +"'";
da = new OleDbDataAdapter(sql, conn);
DataTable dt = new DataTable(); //ds = new DataSet();
da.Fill(dt); //da.Fill(ds, "dbInformation");
dataGridView1.DataSource = dt; //dataGridView1.DataSource = ds.Tables[0];
conn.Close();
}
private void Form1_Load(object sender, EventArgs e)
{
dataGridView2.Columns.Add("Firstname","fname");
dataGridView2.Columns.Add("Lastname","lname");
dataGridView2.Columns.Add("Gender","gender");
retrieveALL();
}
private void btnClear_Click(object sender, EventArgs e)
{
txtFname.Clear();
txtLname.Clear();
txtSearch.Clear();
cbGender.Text = null;
}
private void button1_Click(object sender, EventArgs e)
{
for (int i = 0; i < dataGridView2.Rows.Count; i++)
{
sql = @"INSERT INTO tblProfile (Firstname,Lastname,Gender) VALUES ('"
+ dataGridView2.Rows[i].Cells["Firstname"].Value + "',' "
+ dataGridView2.Rows[i].Cells["Lastname"].Value + "', '"
+ dataGridView2.Rows[i].Cells["Gender"].Value + "')";
dbConnection();
cmd = new OleDbCommand(sql, conn);
cmd.ExecuteNonQuery();
MessageBox.Show("Successfully Added to your Database", "New Record Added", MessageBoxButtons.OK, MessageBoxIcon.Information);
conn.Close();
//btnReadDatabase_Click(sender, e);
retrieveALL();
}
}
}
}
0 comments:
Post a Comment