# Unit - IV ( Hinglish Version )

#### **UNIT IV: Object Serialization aur Remoting, ADO.NET, aur C# Windows Forms ke Data Control**

***

#### **Object Serialization aur Remoting**

Serialization aur remoting .NET ka use karke objects ko store, transmit, aur reconstruct karne ke liye kiya jata hai.

* **Serialization**: Object ko ek aise format me convert karta hai jo save ya transmit ho sake.
* **Remoting**: Applications ke beech communication enable karta hai.

**System.IO Namespace**

**System.IO** namespace data streams aur files ko read, write aur manage karne ke liye types provide karta hai.

1. **Streams:**
   * Stream ek abstract class hai jo data transfer ke liye use hoti hai.
   * Common Streams:
     * `FileStream`: Files ke saath kaam karta hai.
     * `MemoryStream`: Data ko memory me store karta hai.
     * `NetworkStream`: Network par communicate karta hai.
2. **TextWriter aur TextReader:**
   * **TextWriter**: Text data ko stream me write karta hai (e.g., `StreamWriter`).
   * **TextReader**: Text data ko stream se read karta hai (e.g., `StreamReader`).
3.  **BinaryWriter aur BinaryReader:**

    * **BinaryWriter**: Data ko binary format me stream par write karta hai.
    * **BinaryReader**: Stream se binary data read karta hai.

    **Example of Binary Read/Write:**

    ```csharp
    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 aur Formatters**

Serialization objects ko byte stream me convert karta hai jo store ya transmit ho sake, aur deserialization byte stream se object ko reconstruct karta hai.

1. **Serialization ke Types:**
   * **Binary Serialization**: Compact aur efficient storage ke liye `BinaryFormatter` ka use hota hai.
   * **XML Serialization**: Objects ko XML format me convert karta hai (`XmlSerializer` ka use karke).
   * **JSON Serialization**: Objects ko JSON me convert karta hai (`JsonSerializer` ka use karke).
2.  **BinaryFormatter Example:**

    ```csharp
    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 alag-alag application domains, processes, ya machines ke beech communication ke liye kaam karta hai.

1. **Remoting ke Basics:**
   * Objects jaise ki `MarshalByRefObject` ka use hota hai data ko pass karne ke liye.
   * TCP ya HTTP channels ka use karke communicate karta hai.
2. **Remoting Configuration:**
   * **Server-Side**: Remoting service me object ko host karna.
   * **Client-Side**: Server se object consume karna.

***

#### **ADO.NET**

ADO.NET ek data access technology hai jo databases ke saath connect, retrieve, manipulate, aur update karne me madad karta hai.

**Connected aur Disconnected Scenarios**

1. **Connected Scenario:**
   * Database ke saath active connection chahiye hota hai operations ke liye.
   * Objects jaise `SqlConnection`, `SqlCommand`, aur `SqlDataReader` ka use hota hai.
2. **Disconnected Scenario:**
   * Data locally **DataSet** me store hota hai.
   * Offline changes karne ki permission deta hai jo baad me update ki ja sakti hai.

**Key ADO.NET Objects**

1.  **SqlConnection**: Database ke saath connection banata hai.

    ```csharp
    SqlConnection conn = new SqlConnection("your_connection_string");
    conn.Open();
    conn.Close();
    ```
2.  **SqlCommand**: Query ya command execute karne ke liye use hota hai.

    ```csharp
    SqlCommand cmd = new SqlCommand("SELECT * FROM Table", conn);
    ```
3.  **SqlDataReader**: Row-by-row data read karta hai database se.

    ```csharp
    SqlDataReader reader = cmd.ExecuteReader();
    while (reader.Read())
    {
        Console.WriteLine(reader["ColumnName"]);
    }
    ```
4.  **DataSet**: Data ka in-memory representation hai.

    ```csharp
    DataSet ds = new DataSet();
    SqlDataAdapter adapter = new SqlDataAdapter("SELECT * FROM Table", conn);
    adapter.Fill(ds);
    ```
5. **SqlDataAdapter**: Database aur `DataSet` ke beech bridge ka kaam karta hai.

***

#### **C# Windows Forms for Data Control**

Windows Forms alag-alag controls provide karta hai jo data display aur manage karne ke liye use hote hain.

**Key Controls for Data Control**

1. **GridView:**
   * Tabular format me data display karta hai.
   * Data ko `DataSource` ke saath bind kiya ja sakta hai.
2. **DataSource:**
   * Controls (jaise `GridView`) ke liye data source ka kaam karta hai.
3. **DataBinding:**
   * UI controls ko data sources ke saath link karta hai.
   *   Example:

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

**ADO.NET in Windows Forms**

* Controls jaise `GridView` ko `DataSet` ya `DataTable` ke saath connect karta hai.
*   Example:

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

***

#### **Summary**

* **Object Serialization** objects ko save ya transmit karne ke liye convert karta hai, aur formatters jaise `BinaryFormatter` ya `JsonSerializer` ka use hota hai.
* **Remoting** alag processes ya networks ke beech communication enable karta hai.
* **ADO.NET** databases ke saath interact karne ka ek strong framework provide karta hai, jo connected (`SqlCommand`, `SqlDataReader`) aur disconnected (`DataSet`, `SqlDataAdapter`) operations ko support karta hai.
* **C# Windows Forms** controls jaise `GridView` aur databinding mechanisms ke madhyam se data ko user-friendly interface me display aur manage karne deta hai.
