using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Net.NetworkInformation;
namespace WindowsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {

                       
        }

        private void button1_Click(object sender, EventArgs e)
        {

            System.Data.OleDb.OleDbConnection ocn = new System.Data.OleDb.OleDbConnection();
            System.Data.OleDb.OleDbCommand ocm = new System.Data.OleDb.OleDbCommand();
            System.Data.OleDb.OleDbDataAdapter oda = new System.Data.OleDb.OleDbDataAdapter();
            DataTable dt = new DataTable();

            ocn.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + Application.StartupPath + "\\db1.mdb";

            ocm.Connection = ocn;
            ocm.CommandText = "SELECT * FROM t1";

            oda.SelectCommand = ocm;
            oda.Fill(dt);

            dataGridView1.DataSource = dt;

            dt.Dispose();
            oda.Dispose();
            ocm.Dispose();
            ocn.Dispose();
           
        }

        private void button2_Click(object sender, EventArgs e)
        {
            System.Data.OleDb.OleDbConnection ocn = new System.Data.OleDb.OleDbConnection();
            System.Data.OleDb.OleDbCommand ocm = new System.Data.OleDb.OleDbCommand();

            ocn.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + Application.StartupPath + "\\db1.mdb";

            ocm.Connection = ocn;

            //روش اول
            //ocm.CommandText = "INSERT INTO t1 (fname,lname)VALUES('" + textBox1.Text + "','" + textBox2.Text + "')";

            //روش دوم
            ocm.CommandText = "INSERT INTO t1 (fname,lname)VALUES(@p1,@p2)";
            ocm.Parameters.Clear();
            ocm.Parameters.AddWithValue("@p1", textBox1.Text);
            ocm.Parameters.AddWithValue("@p2", textBox2.Text);

            ocn.Open();
            ocm.ExecuteNonQuery();
            ocn.Close();

            ocm.Dispose();
            ocn.Dispose();

        }

        private void button3_Click(object sender, EventArgs e)
        {
            System.Data.OleDb.OleDbConnection ocn = new System.Data.OleDb.OleDbConnection();
            System.Data.OleDb.OleDbCommand ocm = new System.Data.OleDb.OleDbCommand();

            ocn.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + Application.StartupPath + "\\db1.mdb";

            ocm.Connection = ocn;

            ocm.CommandText = "DELETE FROM t1 WHERE scode=@p1";
            ocm.Parameters.Clear();
            ocm.Parameters.AddWithValue("@p1", textBox3.Text);

            ocn.Open();
            ocm.ExecuteNonQuery();
            ocn.Close();

            ocm.Dispose();
            ocn.Dispose();

        }

        private void button4_Click(object sender, EventArgs e)
        {
            System.Data.OleDb.OleDbConnection ocn = new System.Data.OleDb.OleDbConnection();
            System.Data.OleDb.OleDbCommand ocm = new System.Data.OleDb.OleDbCommand();

            ocn.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + Application.StartupPath + "\\db1.mdb";

            ocm.Connection = ocn;

            ocm.CommandText = "UPDATE t1 SET fname=@p1,lname=@p2 WHERE scode=@p3";
            ocm.Parameters.Clear();
            ocm.Parameters.AddWithValue("@p1", textBox5.Text);
            ocm.Parameters.AddWithValue("@p2", textBox6.Text);
            ocm.Parameters.AddWithValue("@p3", textBox4.Text);

            ocn.Open();
            ocm.ExecuteNonQuery();
            ocn.Close();

            ocm.Dispose();
            ocn.Dispose();
        }
    }
}