Tuesday, 27 November 2012

Student Detail using Database in Windows Application

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 windatabase
{
public partial class Form1 : Form
{
OleDbConnection dc = new OleDbConnection();
OleDbCommand dcmd = new OleDbCommand();
OleDbDataReader dr;


public Form1()
{
InitializeComponent();

}

private void clear_Click(object sender, EventArgs e)
{
textBox1.Text = "";
textBox2.Text = "";
textBox3.Text = "";
}

private void Form1_Load(object sender, EventArgs e)
{

dc.ConnectionString = "Provider=Microsoft.JET.OLEDB.4.0;" + "Data Source=E:\\cla.mdb";
dc.Open();
dcmd.Connection = dc;


}

private void save_Click(object sender, EventArgs e)
{
//dataGridView1.Columns.




string name, clas;
int ID=40, age;
name = textBox1.Text;
clas = textBox2.Text;
age = Convert.ToInt16( textBox3.Text);

dcmd.CommandText = "INSERT INTO cla VALUES('"+ID+"','" + name + "','" + clas + "'," +age + ")";
dr = dcmd.ExecuteReader();
dr.Close();
MessageBox.Show("recordsaved");
}

private void delete_Click(object sender, EventArgs e)
{
int ag;
ag = Convert.ToInt16(textBox4.Text);
dcmd.CommandText = "DELETE * FROM cla WHERE age=" + ag + "";
dr = dcmd.ExecuteReader();
dr.Close();
MessageBox.Show("Record Deleted");

}

private void search_Click(object sender, EventArgs e)
{
int vv;
vv = Convert.ToInt16(textBox4.Text);
dcmd.CommandText = "SELECT * FROM cla WHERE age=" + vv + "";
dr = dcmd.ExecuteReader();
dr.Read();
string name = dr.GetString(1);
string clas = dr.GetString(2);
int age = dr.GetInt16(3);
textBox1.Text = name;
textBox2.Text = clas;
textBox3.Text = age.ToString();

dr.Close();
}
}
}

No comments:

Post a Comment