What is ADO.NET?What are the key components of ADO.NET?What are the ADO.NET namespaces?What is the meaning of object pooling?What are the differences between ADO.NET and classic ADO?Explain the architecture of ADO.NET.?What is a Connection String in ADO.NET C#??How do you establish a database connection using ADO.NET?Explain the concept of Connection Pooling in ADO.NET C#.Differentiate between Object pooling and Connection pooling in C#?What is a DataReader in ADO.NET? Explain with C# example?What is the functionality of CommandBehavior.SchemaOnly?Why CommandBehavior.SingleResult flag is used in ADO.NET?What does CommandBehavior.SingleRow do in ADO.NET?How we can get multiple results by DataReader using same connection in C#?How can we force the connection object to close after my DataReader is closed?What is a DataSet in ADO.NET? Explain with C# example?What are typed and un-typed datasets in ADO.NET C#?Write down some of the characteristic of DataSet?What is the difference between dataSet and DataReader?Why is DataSet Slower than DataReader? Explain with Example.How does DataSet handle data in a disconnected environment?What is the Difference between connected and disconnected architectire?Explain HasChanges() method of DataSet in C#.Explain GetChanges() method with detaild C# Example.Explain RejectChanges() method with C# Example.Explain AcceptChanges() method with C# Example.What are the various methods provided by DataSet for XML in C#?What is the purpose of DataAdapter in ADO.NET?Explain the steps involved in retrieving data using DataAdapter.

Typed and Un-typed Datasets in ADO.NET C#

In ADO.NET, when working with databases in C#, you often deal with datasets to represent and manipulate the data. Datasets can be categorized into two types: typed datasets and un-typed datasets. Let's explore each type with complete source code examples and their respective outputs.

Untyped Dataset:

An un-typed dataset is a generic representation of data from a database. It doesn't have a predefined structure, so you access its data using column names as strings. Here's an example of working with an un-typed dataset:


using System;
using System.Data;
using System.Data.SqlClient;

class Program
{
    static void Main()
    {
        string connectionString = "your_connection_string_here";
        string query = "SELECT * FROM Customers";

        using (SqlConnection connection = new SqlConnection(connectionString))
        {
            connection.Open();

            SqlDataAdapter adapter = new SqlDataAdapter(query, connection);
            DataSet dataSet = new DataSet();

            adapter.Fill(dataSet, "Customers");

            foreach (DataRow row in dataSet.Tables["Customers"].Rows)
            {
                Console.WriteLine($"Customer ID: {row["CustomerID"]}, Name: {row["CompanyName"]}");
            }
        }
    }
}
        
    

Output (sample data):


Customer ID: ALFKI, Name: Alfreds Futterkiste
Customer ID: ANATR, Name: Ana Trujillo Emparedados y helados
Customer ID: ... (more data)
        
    

In the un-typed dataset example, we retrieve data from a database and access it using column names as strings.

Typed Dataset:

A typed dataset, on the other hand, provides a strongly-typed representation of your database schema. It's generated using the Visual Studio DataSet Designer, which creates classes for each table and column in your database. Here's an example:

  1. Create a Typed Dataset in Visual Studio:
    • Open your project in Visual Studio.
    • Right-click on your project in Solution Explorer.
    • Add -> New Item.
    • Choose "DataSet" from the templates and name it "MyTypedDataSet.xsd."
    • Drag and drop tables from Server Explorer onto the DataSet Designer surface.
  2. Code to use the Typed Dataset:

using System;
using System.Data;
using YourNamespace.MyTypedDataSetTableAdapters;

class Program
{
    static void Main()
    {
        MyTypedDataSet dataSet = new MyTypedDataSet();
        CustomersTableAdapter customersAdapter = new CustomersTableAdapter();

        customersAdapter.Fill(dataSet.Customers);

        foreach (MyTypedDataSet.CustomersRow row in dataSet.Customers.Rows)
        {
            Console.WriteLine($"Customer ID: {row.CustomerID}, Name: {row.CompanyName}");
        }
    }
}
        
    

Output (sample data):


Customer ID: ALFKI, Name: Alfreds Futterkiste
Customer ID: ANATR, Name: Ana Trujillo Emparedados y helados
Customer ID: ... (more data)
        
    

Advantages and Disadvantages of Typed and Un-typed Datasets

Typed Dataset:

Advantages:
  • Type Safety: Typed datasets provide strong type checking, which helps catch errors before running your program.
  • IntelliSense Support: Your code editor offers suggestions and auto-completions, making coding easier.
  • Code Readability: Predefined classes make your code more readable and self-explanatory.
  • Structured Development: They encourage organized development with generated classes.
Disadvantages:
  • Complexity: Creating typed datasets can be complex, especially for large databases.
  • Increased Code Size: Typed datasets can lead to larger code files, which might be overwhelming for small projects.
  • Rigidity: They are less flexible; if your database schema changes, you'll need to update classes.

Un-typed Dataset:

Advantages:
  • Flexibility: Un-typed datasets are flexible; they can work with various tables and columns without specific classes.
  • Simplicity: They are simpler to set up and use, suitable for small projects or quick data access.
  • Quick Prototyping: Useful for quick prototyping or experimenting with data without creating class structures.
Disadvantages:
  • Lack of Type Safety: Un-typed datasets don't provide compile-time type checking, potentially causing runtime errors.
  • Code Readability: Accessing data using string-based column names may lead to less readable and error-prone code.
  • Limited IntelliSense: Un-typed datasets offer limited code editor support, making it harder to discover available columns and properties.

In summary, the choice between typed and un-typed datasets depends on your project's requirements, considering factors like type safety, code readability, flexibility, and complexity.