Saturday, August 25, 2012

Typed and Un-Typed Dataset


LINQ to Dataset (formely known as DLINQ) supports bot Typed and Untyped DataSet.

Un Typed DataSet:

An "untyped" dataset is an instance of the DataSet class from the System.Data namespace.
It’s called “untyped” because all columns are provided as the base System.Object type (“object” in C# and “Object” in VB.NET)

void Form1_Load(object sender, EventArgs e) 
{  DataSet ds = new DataSet();  sqlDataAdapter1.Fill(ds);  foreach( DataRow row in ds.Tables[0].Rows ) {    string fname = (string)row["au_fname"];    bool contract = (bool)row["contract"];    string item =      string.Format("{0} has a contract: {1}", fname, contract);    listBox1.Items.Add(item);  }}


Typed DataSet:

Typed data sets provide three features that are not part of the base DataSet class are as follows

1. They provide a designer surface for laying out relationships and constraints.
2. They provide type-checked operations, i.e. retrieval, inserts and updates.
3. They provide a special constructor to add serialization support to your typed data set.

A type-safe Dataset, on the other hand, allows you to write the following code:

void Form1_Load(object sender, EventArgs e) 
{  AuthorsDataSet ds = new AuthorsDataSet();  sqlDataAdapter1.Fill(ds);  foreach( AuthorsDataSet.authorsRow row in ds.authors.Rows ) {    string fname = row.au_fname;    bool contract = row.contract;    string item =      string.Format("{0} has a contract: {1}", fname, contract);    listBox1.Items.Add(item);  }}


How to Add Typed DataSet:

1. Right click on Project Name in solution explorer
2. Select Add New Item
3. Select DataSet option which generate the XSD file.
4. On double click on this XSD file you can get designer window for Dataset.