Data base connection using C#| How to Insert, Update, Delete and view data in database using C#

 Data Base Connectivity Using C#:

                                              (INTERFACE)


SOURCE CODE

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 db_connection

{

public partial class Form1 : Form

{

OleDbConnection con = new OleDbConnection(@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\HUSSNAIN MEHDI\Documents\mehdi.accdb");

public Form1()

{

InitializeComponent();

}



private void button1_Click(object sender, EventArgs e)

{

con.Open();

OleDbCommand cmd = con.CreateCommand();

cmd.CommandType = CommandType.Text;

cmd.CommandText="insert into student values('"+textBox1.Text+"' , '"+textBox2.Text+"' , '"+textBox3.Text+"')";

cmd.ExecuteNonQuery();

con.Close();

MessageBox.Show("inserted Successfully");

}
private void button2_Click(object sender, EventArgs e)

{

con.Open();

OleDbCommand cmd = con.CreateCommand();

cmd.CommandType = CommandType.Text;

cmd.CommandText = "delete from student where Name='" + textBox1.Text + "' OR F_Name='" + textBox2.Text + "' OR R_No='" + textBox3.Text + "'";

cmd.ExecuteNonQuery();

con.Close();

MessageBox.Show("deleted successfully");

}



private void button3_Click(object sender, EventArgs e)

{

con.Open();



OleDbCommand cmd = con.CreateCommand();

cmd.CommandType = CommandType.Text;

cmd.CommandText = "update student set Name='" + textBox1.Text + "' , F_Name='" + textBox2.Text + "' where R_No='" + textBox3.Text + "'";

cmd.ExecuteNonQuery();

con.Close();

MessageBox.Show("Updated Successfully");

}



private void button4_Click(object sender, EventArgs e)

{

con.Open();

OleDbCommand cmd = con.CreateCommand();

cmd.CommandType = CommandType.Text;

cmd.CommandText = "select * from student";

cmd.ExecuteNonQuery();

DataTable dt = new DataTable();

OleDbDataAdapter da = new OleDbDataAdapter(cmd);

da.Fill(dt);

dataGridView1.DataSource = dt;

con.Close();

}

}

}

 

 

 

 

 

Post a Comment

Please do not enter any spam link in the comment box