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.SqlClient;

namespace WindowsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            SqlConnection scn = new SqlConnection();
            SqlCommand scm = new SqlCommand();
            SqlDataAdapter sda = new SqlDataAdapter();
            DataTable dt = new DataTable();

            scn.ConnectionString = @"Data Source=.\SQLEXPRESS;AttachDbFilename=" + Application.StartupPath + @"\db.mdf;Integrated Security=True;Connect Timeout=30;User Instance=True";

            scm.Connection = scn;
            scm.CommandText = "SELECT * FROM t1";

            sda.SelectCommand = scm;
            sda.Fill(dt);

            dataGridView1.DataSource = dt;

            dt.Dispose();
            sda.Dispose();
            scm.Dispose();
            scn.Dispose();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            SqlConnection scn = new SqlConnection();
            SqlCommand scm = new SqlCommand();

            scn.ConnectionString = @"Data Source=.\SQLEXPRESS;AttachDbFilename=" + Application.StartupPath + @"\db.mdf;Integrated Security=True;Connect Timeout=30;User Instance=True";

            scm.Connection = scn;
            scm.CommandText = "INSERT INTO t1 (scode,fname,lname)VALUES(@p1,@p2,@p3)";
            scm.Parameters.Clear();
            scm.Parameters.AddWithValue("@p1", textBox1.Text);
            scm.Parameters.AddWithValue("@p2", textBox2.Text);
            scm.Parameters.AddWithValue("@p3", textBox3.Text);

            scn.Open();
            scm.ExecuteNonQuery();
            scn.Close();

            scm.Dispose();
            scn.Dispose();
        }

        private void button3_Click(object sender, EventArgs e)
        {
            SqlConnection scn = new SqlConnection();
            SqlCommand scm = new SqlCommand();

            scn.ConnectionString = @"Data Source=.\SQLEXPRESS;AttachDbFilename=" + Application.StartupPath + @"\db.mdf;Integrated Security=True;Connect Timeout=30;User Instance=True";

            scm.Connection = scn;
            scm.CommandText = "DELETE FROM t1 WHERE scode=@p1";
            scm.Parameters.Clear();
            scm.Parameters.AddWithValue("@p1", textBox4.Text);

            scn.Open();
            scm.ExecuteNonQuery();
            scn.Close();

            scm.Dispose();
            scn.Dispose();
        }

        private void button4_Click(object sender, EventArgs e)
        {
            SqlConnection scn = new SqlConnection();
            SqlCommand scm = new SqlCommand();

            scn.ConnectionString = @"Data Source=.\SQLEXPRESS;AttachDbFilename=" + Application.StartupPath + @"\db.mdf;Integrated Security=True;Connect Timeout=30;User Instance=True";

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

            scn.Open();
            scm.ExecuteNonQuery();
            scn.Close();

            scm.Dispose();
            scn.Dispose();
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }
    }
}