Tuesday, 27 November 2012

Database in Console Application

using System;
using System.Collections.Generic;
using System.Text;
using System.Data.OleDb;

namespace Consodatabase
{
class stumark
{
static void Main(string[] args)
{
OleDbConnection dc = new OleDbConnection();
int f = 1,ch;
string reg,name,clas;
int age;
try
{
dc.ConnectionString = "Provider=Microsoft.JET.OLEDB.4.0;" + "Data Source=f:\\stud.mdb";
dc.Open();
OleDbCommand dcmd = new OleDbCommand();
OleDbDataReader dr;// = dcmd.ExecuteReader();
dcmd.Connection = dc;
while (f == 1)
{
Console.WriteLine("\t1.Add");
Console.WriteLine("\t2.ViewAll");
Console.WriteLine("\t3.Delete");
Console.WriteLine("\t4.Exit");
Console.WriteLine("Enter your choice");
ch = int.Parse(Console.ReadLine());
switch (ch)
{
case 1:
{
Console.WriteLine("Enter Regno");
reg = Console.ReadLine();
Console.WriteLine("Enter Name");
name = Console.ReadLine();
Console.WriteLine("Enter Class");
clas = Console.ReadLine();
Console.WriteLine("Enter Age");
age = int.Parse(Console.ReadLine());
dcmd.CommandText = "INSERT INTO stud VALUES('" + reg + "','" + name + "','" + clas + "'," + age + ")";
dr = dcmd.ExecuteReader();
dr.Close();
break;
}
case 2:
{
dcmd.CommandText = "SELECT * FROM stud";
dr = dcmd.ExecuteReader();
Console.WriteLine("RegNo\tName\tClass\tAge");
while (dr.Read())
{
reg = dr.GetString(0);
name = dr.GetString(1);
clas = dr.GetString(2);
age = dr.GetInt32(3);
Console.WriteLine("{0}\t{1}\t{2}\t{3}", reg, name, clas, age);
}
dr.Close();
break;
}
case 3:
{
Console.WriteLine("Enter reg no to Delete");
string re=Console.ReadLine();
dcmd.CommandText="DELETE * FROM stud WHERE reg='"+re+"'";
dr=dcmd.ExecuteReader();
dr.Close();
break;
}

default:
f = 0;
break;
}
}
}
catch (Exception e)
{
Console.WriteLine("Error" + e.Message);
}
finally
{
dc.Close();
}


}
}
}

No comments:

Post a Comment