Unit - IV ( English Version )

Object Serialization and Remoting

Serialization and remoting in .NET allow objects to be stored, transmitted, and reconstructed in different environments. Serialization converts an object into a format that can be saved or transmitted, while remoting enables communication between applications.

System.IO Namespace

The System.IO namespace provides types for reading, writing, and managing data streams and files.

  1. Streams:

    • A Stream is an abstract class used to transfer data.

    • Common streams:

      • FileStream: Reads and writes data to files.

      • MemoryStream: Stores data in memory.

      • NetworkStream: Communicates over a network.

  2. TextWriter and TextReader:

    • TextWriter: Writes text data to a stream (e.g., StreamWriter).

    • TextReader: Reads text data from a stream (e.g., StreamReader).

  3. BinaryWriter and BinaryReader:

    • BinaryWriter: Writes data in binary format to a stream.

    • BinaryReader: Reads binary data from a stream.

    Example of Binary Read/Write:

    using (BinaryWriter writer = new BinaryWriter(File.Open("data.bin", FileMode.Create)))
    {
        writer.Write(42);
        writer.Write("Hello");
    }
    
    using (BinaryReader reader = new BinaryReader(File.Open("data.bin", FileMode.Open)))
    {
        Console.WriteLine(reader.ReadInt32());
        Console.WriteLine(reader.ReadString());
    }

Serialized Object Persistence and Formatters

Serialization converts an object into a byte stream for storage or transmission, and deserialization reconstructs the object from the byte stream.

  1. Types of Serialization:

    • Binary Serialization: Uses BinaryFormatter for compact, efficient storage.

    • XML Serialization: Converts objects into XML format using XmlSerializer.

    • JSON Serialization: Converts objects into JSON using JsonSerializer.

  2. BinaryFormatter Example:

    using System.Runtime.Serialization.Formatters.Binary;
    using System.IO;
    
    BinaryFormatter formatter = new BinaryFormatter();
    using (FileStream fs = new FileStream("objectData.bin", FileMode.Create))
    {
        MyClass obj = new MyClass { Id = 1, Name = "Test" };
        formatter.Serialize(fs, obj);
    }
    
    using (FileStream fs = new FileStream("objectData.bin", FileMode.Open))
    {
        MyClass obj = (MyClass)formatter.Deserialize(fs);
    }

Remoting

Remoting enables communication between different application domains, processes, or machines.

  1. Remoting Basics:

    • Uses objects like MarshalByRefObject to pass data between applications.

    • Communication can be achieved using channels like TCP or HTTP.

  2. Remoting Configuration:

    • Server-Side: Host an object in a remoting service.

    • Client-Side: Consume the object from the server.


ADO.NET

ADO.NET is the data access technology used to connect, retrieve, manipulate, and update data in databases. It supports both connected and disconnected scenarios.

Connected and Disconnected Scenarios

  1. Connected Scenario:

    • Requires an active connection to the database for performing operations.

    • Uses objects like SqlConnection, SqlCommand, and SqlDataReader.

  2. Disconnected Scenario:

    • Works with data stored locally in a DataSet.

    • Allows changes to be made offline and updated later.

Key ADO.NET Objects

  1. SqlConnection: Represents a connection to a database.

    SqlConnection conn = new SqlConnection("your_connection_string");
    conn.Open();
    conn.Close();
  2. SqlCommand: Represents a query or command executed on the database.

    SqlCommand cmd = new SqlCommand("SELECT * FROM Table", conn);
  3. SqlDataReader: Reads data row by row from a database.

    SqlDataReader reader = cmd.ExecuteReader();
    while (reader.Read())
    {
        Console.WriteLine(reader["ColumnName"]);
    }
  4. DataSet: An in-memory representation of data.

    DataSet ds = new DataSet();
    SqlDataAdapter adapter = new SqlDataAdapter("SELECT * FROM Table", conn);
    adapter.Fill(ds);
  5. SqlDataAdapter: Acts as a bridge between the database and the DataSet.


C# Windows Forms for Data Control

Windows Forms provides various controls for displaying and managing data.

Key Controls for Data Control

  1. GridView:

    • Displays data in a tabular format.

    • Data can be bound to a DataSource.

  2. DataSource:

    • Acts as a source of data for controls like GridView.

  3. DataBinding:

    • Links UI controls to data sources.

    • Example:

      textBox1.DataBindings.Add("Text", dataTable, "ColumnName");

ADO.NET in Windows Forms

  • Connect controls like GridView to a DataSet or DataTable.

  • Example:

    DataSet ds = new DataSet();
    SqlDataAdapter adapter = new SqlDataAdapter("SELECT * FROM Table", conn);
    adapter.Fill(ds);
    dataGridView1.DataSource = ds.Tables[0];

Summary

  • Object Serialization converts objects into a format for storage or transmission, using formatters like BinaryFormatter or JsonSerializer.

  • Remoting enables applications to communicate across processes or networks.

  • ADO.NET provides a robust way to connect to and manipulate databases, supporting both connected (SqlCommand, SqlDataReader) and disconnected (DataSet, SqlDataAdapter) operations.

  • C# Windows Forms allows data to be displayed and managed in a user-friendly interface using controls like GridView and databinding mechanisms.

Last updated