Serialization capabilities of DataSet : DataSet « ADO.net Database « ASP.NET Tutorial






<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" 
    Inherits="Default"%>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>Test DataSet Serialization</title>
</head>
<body>
    <div id="pageContent">
        <form id="form1" runat="server">
            <table>
                <tr>
                    <td><asp:Button Text="Serialize as XML" runat="server" ID="XmlButton" OnClick="XmlButton_Click" Width="200px" /></td>
                    <td><asp:Label ID="XmlSize" runat="server" /></td>
                </tr>
                <tr>
                    <td><asp:Button Text="Serialize as Binary" runat="server" ID="BinButton" OnClick="BinButton_Click" Width="200px" /></td>
                    <td><asp:Label ID="BinSize" runat="server" /></td>
                </tr>
            </table>
        </form>
    </div>
</body>
</html>

File: Default.aspx.cs

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Data.SqlClient;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;


public partial class Default : System.Web.UI.Page
{
  private string XmlFile = @"c:\serial.xml";
  private string BinFile = @"c:\serial.bin";

  private DataSet GetData()
  {
    DataSet ds = new DataSet();
    SqlDataAdapter adapter = new SqlDataAdapter(
      "SELECT * FROM [order details]",
      ConfigurationManager.ConnectionStrings["NorthwindConnectionString"].ConnectionString);
    adapter.Fill(ds);

    return ds;
  }

  protected void XmlButton_Click(object sender, EventArgs e)
  {
    DataSet ds = GetData();
    ds.RemotingFormat = SerializationFormat.Xml;

    StreamWriter writer = new StreamWriter(XmlFile);
    BinaryFormatter bin = new BinaryFormatter();
    bin.Serialize(writer.BaseStream, ds);
    writer.Close();

    FileInfo fi = new FileInfo(XmlFile);
    XmlSize.Text = (fi.Length/1024).ToString() + " KB";
  }

  protected void BinButton_Click(object sender, EventArgs e)
  {
    DataSet ds = GetData();
    ds.RemotingFormat = SerializationFormat.Binary;

    StreamWriter writer = new StreamWriter(BinFile);
    BinaryFormatter bin = new BinaryFormatter();
    bin.Serialize(writer.BaseStream, ds);
    writer.Close();

    FileInfo fi = new FileInfo(BinFile);
    BinSize.Text = (fi.Length/1024).ToString() + " KB";
  }
}








18.27.DataSet
18.27.1.The DataSet object represents an in-memory database.
18.27.2.Fill a DataSet
18.27.3.Iterating Through A DataSet
18.27.4.Fill DataSet with SqlDataAdapter
18.27.5.Use OleDbDataAdapter to fill DataSet
18.27.6.Iterating Through A DataSet from MySQL database
18.27.7.List Binding To A DataSet
18.27.8.Pulling Single Values From Dataset Bounded Lists
18.27.9.Create DataSet by your own
18.27.10.Serialization capabilities of DataSet