Saturday, April 20, 2013

Access Data Base Connection & DDL Functions


  Class.cs
 using System;
  using System.Collections.Generic;
  using System.Linq;
  using System.Text;
  using System.Data.OleDb;
  using System.Data;

  namespace WindowsFormsApplication1
  {
  class Class1
  {
  public static OleDbConnection getConnection()
  {
OleDbConnection con = new OleDbConnection(@"Provider=Microsoft.ACE.OLEDB.12.0;Data   Source=C:\Users\Spring2013\Documents\Database1.accdb");
       con.Open();
       return con;
  }
  public static DataTable gettable(string destr)
  {
  OleDbConnection con = getConnection();
  OleDbDataAdapter da = new OleDbDataAdapter();
  OleDbCommand cmd = new OleDbCommand(destr, con);
  da.SelectCommand = cmd;
  DataTable dt = new DataTable();
  da.Fill(dt);
  con.Close();
  return dt;
  }
  public static void ExecuteQuery(string str)
  {
  OleDbConnection con = getConnection();
  OleDbCommand cmd = new OleDbCommand(str, con);
  cmd.ExecuteNonQuery();
  con.Close();
 }
 }
 }
Form1.cs
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 WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        DataTable dt;
        int index;
        public Form1()
        {
            InitializeComponent();
            dt = Class1.gettable("select * from Info");
            index = 0;
            if (dt.Rows.Count > 0)
            {
                setValues(index);
            }
        }
        public void setValues(int index)
        {
            DataRow dr = dt.Rows[index];
            textBox1.Text = dr["ID"].ToString();
            textBox2.Text = dr["Student Name"].ToString();
            textBox3.Text = dr["Semester"].ToString();
        }
        private void NewBtn_Click(object sender, EventArgs e)
        {
             New();
        }
        private void NextRecord_Click(object sender, EventArgs e)
        {
            Next();
        }
        private void Forward_Click(object sender, EventArgs e)
        {
            FastForward();
        }
        private void ReverseRec_Click(object sender, EventArgs e)
        {
            Reverse();
        }
        private void Delete_Click(object sender, EventArgs e)
        {
            Delete();
        }
        private void Insert_Click(object sender, EventArgs e)
        {
            Insert();
        }
        private void Updater_Click(object sender, EventArgs e)
        {
            Updation();
        }
        public void Reverse()
        {
            index = 0;
            setValues(index);
        }
        public void Next()
        {
            if (index < dt.Rows.Count - 1)
            {
                index++;
                setValues(index);
            }
            else
            {
                index = 0;
                setValues(index);
            }
        }
        public void FastForward()
        {
            index = dt.Rows.Count-1;
            setValues(index);
        }
        public void Delete()
        {
            string str = "delete from Info where ID = " + textBox1.Text;
            Class1.ExecuteQuery(str);
            MessageBox.Show("Record Deleted");
            dt = Class1.gettable("select * from Info");
            index = 0;
            setValues(index);
        }
        public void Insert()
        {
            string str = "insert into Info Values(" + textBox1.Text + ",'" + textBox2.Text + "','" + textBox3.Text + "')";
            Class1.ExecuteQuery(str);
            MessageBox.Show("Record Entered");
            dt = Class1.gettable("select * from Info");
            index = dt.Rows.Count - 1;
            setValues(index);
        }
        public void Updation()
        {
           string str = "Update Info set [Student Name]='" + textBox2.Text + "' , Semester = '" + textBox3.Text + "' where ID = " + textBox1.Text;
           Class1.ExecuteQuery(str);
           MessageBox.Show("Record updated");
           dt = Class1.gettable("select * from Info");
           setValues(index);
         }
        public void New()
        {
            textBox1.Text = (Convert.ToInt32(dt.Rows[dt.Rows.Count - 1]["ID"]) + 1).ToString();
            textBox2.Text = "";
            textBox3.Text = "";
        }
      }
    }
OUTPUT


No comments:

Post a Comment