Output DataSet as Binary file : DataColumn « ADO.Net « C# / CSharp Tutorial






using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;

  class Program
  {
    static void Main(string[] args)
    {
      DataSet carsInventoryDS = new DataSet("Car");
      
      carsInventoryDS.ExtendedProperties["TimeStamp"] = DateTime.Now;
      carsInventoryDS.ExtendedProperties["DataSetID"] = Guid.NewGuid();
      carsInventoryDS.ExtendedProperties["Company"] = "Training";

      DataColumn carIDColumn = new DataColumn("CarID", typeof(int));
      carIDColumn.Caption = "Car ID";
      carIDColumn.ReadOnly = true;
      carIDColumn.AllowDBNull = false;
      carIDColumn.Unique = true;
      carIDColumn.AutoIncrement = true;
      carIDColumn.AutoIncrementSeed = 0;
      carIDColumn.AutoIncrementStep = 1;

      DataColumn carMakeColumn = new DataColumn("Make", typeof(string));
      DataColumn carColorColumn = new DataColumn("Color", typeof(string));
      DataColumn carPetNameColumn = new DataColumn("PetName", typeof(string));
      carPetNameColumn.Caption = "Pet Name";

      DataTable inventoryTable = new DataTable("Inventory");
      inventoryTable.Columns.AddRange(new DataColumn[] { carIDColumn, carMakeColumn, carColorColumn, carPetNameColumn });

      DataRow carRow = inventoryTable.NewRow();
      carRow["Make"] = "BMW";
      carRow["Color"] = "Black";
      carRow["PetName"] = "Hamlet";
      inventoryTable.Rows.Add(carRow);

      carRow = inventoryTable.NewRow();
      carRow[1] = "A";
      carRow[2] = "B";
      carRow[3] = "C";
      inventoryTable.Rows.Add(carRow);

      inventoryTable.PrimaryKey = new DataColumn[] { inventoryTable.Columns[0] };
      carsInventoryDS.Tables.Add(inventoryTable);
      
      carsInventoryDS.RemotingFormat = SerializationFormat.Binary;

      FileStream fs = new FileStream("BinaryCars.bin", FileMode.Create);
      BinaryFormatter bFormat = new BinaryFormatter();
      bFormat.Serialize(fs, carsInventoryDS);
      fs.Close();

      carsInventoryDS.Clear();

      fs = new FileStream("BinaryCars.bin", FileMode.Open);
      DataSet data = (DataSet)bFormat.Deserialize(fs);
      
    }
  }








32.37.DataColumn
32.37.1.Use a relationship with a calculated column
32.37.2.Set auto increment, seed and step for primary key column
32.37.3.Output DataSet as Binary file
32.37.4.Find data by primary key, if not found add to database
32.37.5.Creating an Autoincrementing Primary Key
32.37.6.Add an expression column to the table