0% found this document useful (0 votes)
18 views

C#Database Connectivity

Uploaded by

sg391405
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views

C#Database Connectivity

Uploaded by

sg391405
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

C#.

Net Database Connectivity and DataGridView Control:

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

namespace DB_Connect1
{
public partial class Form1 : Form
{
OleDbConnection con = new
OleDbConnection(@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\Hall
211\Desktop\DB_Connect1.accdb");
OleDbCommand cmd = new OleDbCommand();
OleDbDataAdapter da = new OleDbDataAdapter();
DataSet ds = new DataSet();
public Form1()
{
InitializeComponent();
}

private void Form1_Load(object sender, EventArgs e)


{
//MessageBox.Show("Connection Established");
show();

private void button1_Click(object sender, EventArgs e)


{
cmd.CommandText = "insert into Table1(ID,Name,Address,Mob)values(" +
textBox1.Text + ",'" + textBox2.Text + "','" + textBox3.Text + "'," +
textBox4.Text + ")";
cmd.Connection = con;
con.Open();
dataGridView1.DataSource = null;
cmd.ExecuteNonQuery();
dataGridView1.DataSource = null;
con.Close();
MessageBox.Show("Record Saved Successfully");
clear();
show();
}

private void button3_Click(object sender, EventArgs e)


{
cmd.CommandText = "update Table1 set Name='" + textBox2.Text +
"',Address='" + textBox3.Text + "',Mob=" + textBox4.Text + " where ID=" +
textBox1.Text + "";
cmd.Connection = con;
con.Open();
cmd.ExecuteNonQuery();
con.Close();
MessageBox.Show("Record Updated Successfully");
show();
}

private void button2_Click(object sender, EventArgs e)


{
cmd.CommandText = "delete from Table1 where ID=" + textBox1.Text +
"";
cmd.Connection = con;
con.Open();
cmd.ExecuteNonQuery();
con.Close();
MessageBox.Show("Record Deleted Successfully");
show();
}
void clear()
{
textBox1.Clear();
textBox2.Clear();
textBox3.Clear();
textBox4.Clear();
}
void show()
{
ds = new DataSet();
cmd.CommandText = "select *from Table1";
cmd.Connection = con;
con.Open();
da = new OleDbDataAdapter(cmd.CommandText, con);
da.Fill(ds);
dataGridView1.DataSource = ds.Tables[0];
con.Close();
}

private void button4_Click(object sender, EventArgs e)


{
this.Close();
}
}
}
MS-Access Database

Output:

You might also like