DataSet Object in ADO.Net 2.0
DataSet Object in ADO.Net 2.0
The dataset object is an in-memory representation of data
The dataset object has a collection of dataTables and dataRelation objects
The dataset can contain one or more dataTables
Example:
The following example shows how you can have two dataTables in a single dataset and add a dataRelation
Add this line above the page_load
Imports System.data
Add this to the page_load
'create new dataset
Dim ShoppingCart As New DataSet("Cart")
'create dataTable
Dim products As DataTable = ShoppingCart.Tables.Add("Products")
'add columns
products.Columns.Add("product_ID", GetType(Guid))
products.Columns.Add("ProductName", GetType(String))
products.Columns.Add("Price", GetType(Integer))
products.Columns.Add("cat_id", GetType(Guid))
'add primary key
products.PrimaryKey = New DataColumn() {products.Columns("product_ID")}
'create second dataTable
Dim category As DataTable = ShoppingCart.Tables.Add("category")
'add columns
category.Columns.Add("cat_id", GetType(Guid))
category.Columns.Add("catName", GetType(String))
'add primary key
category.PrimaryKey = New DataColumn() {category.Columns("cat_id")}
'create data relation
ShoppingCart.Relations.Add("shoppingcart_Products", _
category.Columns("cat_id"),products.Columns("cat_id") )
Add some sample data
'add data
Dim shop, cat As Guid
'get new guids
shop = Guid.NewGuid()
cat = Guid.NewGuid()
'add row to category table
category.Rows.Add(cat, "household")
'Add row to products
products.Rows.Add(shop, "toaster", 25.21, cat)
'get another guid
shop = Guid.NewGuid()
'Add another row to products
products.Rows.Add(shop, "microwave", 250.0, cat)
'get another guid
shop = Guid.NewGuid()
'Add another row to products
products.Rows.Add(shop, "Silverware", 27.5, cat)
'get two new guids
cat = Guid.NewGuid()
shop = Guid.NewGuid()
'add another row to category
category.Rows.Add(cat, "electronics")
'Add another row to products
products.Rows.Add(shop, "television", 300.0, cat)
'get another guid
shop = Guid.NewGuid()
'Add another row to products
products.Rows.Add(shop, "VCR", 250.0, cat)
Add 2 gridVIews to the webform
Add this code
'bind to first gridView
GridView1.DataSource = ShoppingCart
GridView1.DataMember = "products"
GridView1.DataBind()
'bind to second gridView
GridView2.DataSource = ShoppingCart
GridView2.DataMember = "category"
GridView2.DataBind()
Since a dataset can have more than one dataTable you must set the datasource to the dataset and the dataMember to each dataTable

Serialize dataSet as XML
Use the dataset’s writeXML method to serialize a dataset to XMLAdd this code
ShoppingCart.WriteXml(MapPath("Cart.xml"))
Here is the XML is created:
<?xml version="1.0" standalone="yes"?>
<Cart>
<Products>
<product_ID>9e5ff882-9f22-4ca2-a114-84b93abac05d</product_ID>
<ProductName>toaster</ProductName>
<Price>25</Price>
<cat_id>231a895d-0094-4cb5-bd82-c1ac40c1f0e9</cat_id>
</Products>
<Products>
<product_ID>4a13fd47-cfd1-4b78-a516-ea7c069b2ea2</product_ID>
<ProductName>microwave</ProductName>
<Price>250</Price>
<cat_id>231a895d-0094-4cb5-bd82-c1ac40c1f0e9</cat_id>
</Products>
<Products>
<product_ID>5ae1f57d-8ee1-47f8-b32c-9629cb64e9ec</product_ID>
<ProductName>Silverware</ProductName>
<Price>28</Price>
<cat_id>231a895d-0094-4cb5-bd82-c1ac40c1f0e9</cat_id>
</Products>
<Products>
<product_ID>9a14e5e7-c569-4ae1-8e76-a2809a6a8ed8</product_ID>
<ProductName>television</ProductName>
<Price>300</Price>
<cat_id>c35ef4d4-b702-4fec-89b8-8620a24ffd24</cat_id>
</Products>
<Products>
<product_ID>84c059a2-0c18-472a-8bf2-3c1e5c679dd0</product_ID>
<ProductName>VCR</ProductName>
<Price>250</Price>
<cat_id>c35ef4d4-b702-4fec-89b8-8620a24ffd24</cat_id>
</Products>
<category>
<cat_id>231a895d-0094-4cb5-bd82-c1ac40c1f0e9</cat_id>
<catName>household</catName>
</category>
<category>
<cat_id>c35ef4d4-b702-4fec-89b8-8620a24ffd24</cat_id>
<catName>electronics</catName>
</category>
</Cart>
Adding DataType info to the XML file
Change the last line to thisShoppingCart.WriteXml(MapPath("Cart2.xml"), XmlWriteMode.WriteSchema)
You can also put the schema info into a separate file with the following code
ShoppingCart.WriteXml(MapPath("Cart.xml"))
ShoppingCart.WriteXmlSchema(MapPath("cart.xsd"))
If you double-click on this file in solution explorer you see

Serialize dataset as diffGram
A diffgram is an XML file with all the data from your datasetIt also has the original datarow info
Add this code to create a diffgram:
ShoppingCart.WriteXml(MapPath("cartDiffGram.xml"), _
XmlWriteMode.DiffGram)
A diffgram has all the datarow version info, whether rows have been added, changed etc.
Deserializing from a diffGram
You can use a diffgram to create a dataset from an XML fileDim fromXml As New DataSet()
'create structure
fromXml.ReadXmlSchema(MapPath("cart.xsd"))
'populate from the XML file
fromXml.ReadXml(MapPath("cartDiffGram.xml"))
'bind to a third gridView
GridView3.DataSource = fromXml
GridView3.DataBind()