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

C# Database

The document contains C# code for a Windows Forms application that connects to an Access database and performs CRUD (create, read, update, delete) operations. It defines a form class with methods to open a connection, retrieve data and display it in a datagrid, insert new records, update records, search by id, and filter by gender. Buttons on the form trigger these database operations through click event handlers.

Uploaded by

alehegn manaye
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
60 views

C# Database

The document contains C# code for a Windows Forms application that connects to an Access database and performs CRUD (create, read, update, delete) operations. It defines a form class with methods to open a connection, retrieve data and display it in a datagrid, insert new records, update records, search by id, and filter by gender. Buttons on the form trigger these database operations through click event handlers.

Uploaded by

alehegn manaye
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

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 RADproject1
{
public partial class DBtestform : Form
{

public DBtestform()
{
InitializeComponent();
}

private void DBtestform_Load(object sender, EventArgs e)


{

OleDbConnection con1 = new OleDbConnection();


con1.ConnectionString =
@" Provider=Microsoft.ACE.OLEDB.12.0;
Data Source =D:\raddb.accdb;
Persist Security info =False;";
con1.Open();

// if (conn.State.Equals(true))
// {
// MessageBox.Show("connected");
// }

string str1 = "select * from stud;";


OleDbDataAdapter da = new
OleDbDataAdapter(str1, con1);

DataTable dt = new DataTable();


da.Fill(dt);
dataGridView1.DataSource = dt;

private void button1_Click(object sender, EventArgs e)


{
MessageBox.Show("My first Message", "Message", MessageBoxButtons.YesNoCancel);

private void button2_Click(object sender, EventArgs e)


{
OleDbConnection con1 = new OleDbConnection();
con1.ConnectionString =
@" Provider=Microsoft.ACE.OLEDB.12.0;
Data Source =D:\raddb.accdb;
Persist Security info =False;";
con1.Open();

private void button3_Click(object sender, EventArgs e)


{
string id = textBox2.Text.ToString();
string name = textBox3.Text.ToString();
string sex = comboBox1.Text.ToString();;
int Age = int.Parse(textBox4.Text);
//int phone = int.Parse(textBox5.Text);

OleDbConnection conn = new OleDbConnection();


conn.ConnectionString =
@" Provider=Microsoft.ACE.OLEDB.12.0;
Data Source =D:\raddb.accdb;
Persist Security info =False;";
conn.Open();
try
{
string qry = "Insert into stud values ('" + textBox2.Text + "','" + textBox3.Text + "', '" +
comboBox1.Text + "','" + textBox4.Text + "','" + textBox5.Text + "' );";
OleDbCommand result = new OleDbCommand(qry, conn);
result.ExecuteNonQuery();
MessageBox.Show("Data added successfully");
}
catch (DataException e1 )
{
MessageBox.Show("Error" + e1.GetBaseException());
}
}

private void button6_Click(object sender, EventArgs e)


{
OleDbConnection con4 = new OleDbConnection();
con4.ConnectionString =
@" Provider=Microsoft.ACE.OLEDB.12.0;
Data Source =D:\raddb.accdb;
Persist Security info =False;";

con4.Open();

string str4 = "select * from stud where id='"+textBox1.Text+"' ";


OleDbDataAdapter da4 = new OleDbDataAdapter(str4, con4);

DataTable dt4 = new DataTable();

da4.Fill(dt4);

dataGridView1.DataSource = dt4;

private void button4_Click(object sender, EventArgs e)


{
OleDbConnection conn = new OleDbConnection();
conn.ConnectionString =
@" Provider=Microsoft.ACE.OLEDB.12.0;
Data Source =D:\raddb.accdb;
Persist Security info =False;";
conn.Open();
try
{
string qry = "update stud set name= '" + textBox3.Text + "', sex='" + comboBox1.Text + "',
age='" + textBox4.Text + "' Where id ='" +textBox2.Text +"';";
OleDbCommand result = new OleDbCommand(qry, conn);
result.ExecuteNonQuery();
MessageBox.Show("Data updated successfully");
}
catch ( ArgumentOutOfRangeException)
{

}
}

private void button5_Click(object sender, EventArgs e)


{

private void showFemalesToolStripMenuItem_Click(object sender, EventArgs e)


{
OleDbConnection con5 = new OleDbConnection();
con5.ConnectionString =
@" Provider=Microsoft.ACE.OLEDB.12.0;
Data Source =D:\raddb.accdb;
Persist Security info =False;";
con5.Open();

string str4 = "Select * from stud WHERE sex = 'F' ; ";


OleDbDataAdapter da5 = new OleDbDataAdapter(str4, con5);

DataTable dt5 = new DataTable();

da5.Fill(dt5);

dataGridView1.DataSource = dt5;
}

private void showMalesToolStripMenuItem_Click(object sender, EventArgs e)


{
OleDbConnection con5 = new OleDbConnection();
con5.ConnectionString =
@" Provider=Microsoft.ACE.OLEDB.12.0;
Data Source =D:\raddb.accdb;
Persist Security info =False;";

con5.Open();

string str4 = "Select * from stud WHERE sex = 'M' ; ";


OleDbDataAdapter da5 = new OleDbDataAdapter(str4, con5);

DataTable dt5 = new DataTable();

da5.Fill(dt5);

dataGridView1.DataSource = dt5;
}

private void localDBPageToolStripMenuItem_Click(object sender, EventArgs e)


{
local_dbpage ldp = new local_dbpage();
ldp.Show();
this.Hide();
minmaxidentifier mm = new minmaxidentifier();
mm.Hide();
}

private void employeeReportToolStripMenuItem_Click(object sender, EventArgs e)


{
employeereportpage erp = new employeereportpage();
erp.Show();
}
}
}

You might also like