Working with a connection less architecture with help of dataset
1)dataset is a predefined class, which is the part of system.data namespace
2)dataset supports connection less architecture i.e. an opened connection is not required while working with dataset
3)in between dataset and database, there will be no direct communication
4)hence dataadapter is required to transfer the data in between dataset & database
5)dataadapter is used for transferring the data not for holding the data
6)dataset is connection-less but data adapter is connection oriented
7)dataset holds a collection of tables, where every table will be identified will an index number, where always index begins from 0. Optionally we can give tables alias names
8)dataset supports to creat relations with the help of primary and foreign keys
9)dataset stores the data in client machine memory
10)dataset works with the help of XML concepts
Example on dataset
A program to prove that dataset is a)connection-less b)collection tables c)XML based
Open windows forms app project
Start->programs->Microsoft visual studio 2010->Microsoft Visual studio 2010->file menu->new->
project->select visual c# from installed templates->select windows forms application project
place 4 buttons with text get, table1, table2, XML
place datagridview control
using System.Data.sqlclient;
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 WindowsFormsApplication21
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
DataSet ds = new DataSet();
private void Form1_Load(object sender, EventArgs e)
{
}
private void button1_Click(object sender, EventArgs e)
{
SqlConnection cn = new SqlConnection("userid=sa;password=123;database=northwind");
SqlDataAdapter d1 = new SqlDataAdapter("select * from products", cn);
SqlDataAdapter d2 = new SqlDataAdapter("select * from products", cn);
d1.Fill(ds, "pr");
d2.Fill(ds, "or");
MessageBox.Show("dataset is ready");
MessageBox.Show("conn is" + cn.State.ToString());
}
private void button2_Click(object sender, EventArgs e)
{
dataGridView1.DataSource = ds.Tables["pr"];
}
private void button3_Click(object sender, EventArgs e)
{
dataGridView1.DataSource = ds.Tables["or"];
}
private void button4_Click(object sender, EventArgs e)
{
ds.WriteXml("c:\data244.xml");
MessageBox.Show("file is created");
}
}
}
Observation:
The modifications of dataset will not be reflected into database. But these modifications are stored in xml file