Friday, April 20, 2012

ADO.NET - Dataset vs DataReader (Sample Program)

Using dataset:

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

namespace WindowsFormsApplication3
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        private SqlConnection cn;
        private SqlCommand cmd1, cmd2, cmd3,cmd4;

        private void Form1_Load(object sender, EventArgs e)
        {
            cn = new SqlConnection("server=MUHIL-PC;database=student;uid=sa;pwd=focus1");

            cn.Open();
        }
//SELECT
        private void button1_Click(object sender, EventArgs e)
        {
           // cmd1 = new SqlCommand("select * from stud", cn);
           // SqlDataReader dr = cmd1.ExecuteReader();
           // listBox1.Items.Clear();                 
           // while (dr.Read())
            //{
             //listBox1.Items.Add (dr[0].ToString ()+":"+dr[1].ToString ()+":"+dr[2].ToString ()); 
             
           // }
            //dr.Close();
            SqlDataAdapter da = new SqlDataAdapter("select * from stud", cn);
            DataSet ds=new DataSet();
            da.Fill(ds,"stud");
            dataGridView1.DataSource=ds.Tables["stud"];
            dataGridView1.Show();
          }
//INSERT
        private void button2_Click(object sender, EventArgs e)
        {
            cmd2= new SqlCommand("insert into stud values("+textBox1.Text+",'"+textBox2.Text+"',"+textBox3.Text+")",cn);
            cmd2.ExecuteNonQuery();
           
        }
//DELETE
        private void button3_Click(object sender, EventArgs e)
        {
            cmd3 = new SqlCommand("delete from stud where sid= "+ textBox1.Text, cn);
            cmd3.ExecuteNonQuery();
        }
//UPDATE
        private void button4_Click(object sender, EventArgs e)
        {
            cmd4 = new SqlCommand("update stud set sname='"+textBox2.Text+"',ph="+textBox3.Text+" where sid="+textBox1.Text, cn);
            cmd4.ExecuteNonQuery();
        }
}
}

using DataReader:

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

namespace WindowsFormsApplication3
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        private SqlConnection cn;
        private SqlCommand cmd1, cmd2, cmd3,cmd4;

        private void Form1_Load(object sender, EventArgs e)
        {
            cn = new SqlConnection("server=MUHIL-PC;database=student;uid=sa;pwd=focus1");
            cn.Open();
        }
//SELECT
        private void button1_Click(object sender, EventArgs e)
        {
            cmd1 = new SqlCommand("select * from stud", cn);
            SqlDataReader dr = cmd1.ExecuteReader();
            listBox1.Items.Clear();
            while (dr.Read())
            {
             listBox1.Items.Add (dr[0].ToString ()+":"+dr[1].ToString ()+":"+dr[2].ToString ()); 
            }
            dr.Close();
        }
//INSERT
        private void button2_Click(object sender, EventArgs e)
        {
            cmd2= new SqlCommand("insert into stud values("+textBox1.Text+",'"+textBox2.Text+"',"+textBox3.Text+")",cn);
            cmd2.ExecuteNonQuery();
          }
//DELETE 
        private void button3_Click(object sender, EventArgs e)
        {
            cmd3 = new SqlCommand("delete from stud where sid= "+ textBox1.Text, cn);
            cmd3.ExecuteNonQuery();
        }
//UPDATE
        private void button4_Click(object sender, EventArgs e)
        {
            cmd4 = new SqlCommand("update stud set sname='"+textBox2.Text+"',ph="+textBox3.Text+" where sid="+textBox1.Text, cn);
            cmd4.ExecuteNonQuery();
        }   
}
}




No comments:

Post a Comment