ADO - SqlDataTable Class

The ADO.NET DataTable class in C# is a part of the ADO.NET library and represents an in-memory representation of tabular data. It consists of columns and rows that can store and manipulate data.

What is ADO.NET DataTable in C#?

The ADO.NET DataTable class represents a table of data in memory. It provides a structure to hold data in rows and columns, similar to a database table. It is used as a primary container for data retrieved from a data source or as a data source for data-bound controls.

Constructors of ADO.NET DataTable class in C#:

The DataTable class has several constructors to create an instance of a DataTable object. Some commonly used constructors include:

  • DataTable(): Initializes a new instance of the DataTable class with no arguments.
  • DataTable(string tableName): Initializes a new instance of the DataTable class with the specified table name.

Properties of ADO.NET DataTable in C#:

  • The DataTable class has various properties that allow you to access and manipulate its structure and data. Some important properties include:
  • Columns: Gets the collection of DataColumn objects that define the schema of the table.
  • Rows: Gets the collection of DataRow objects that contain the data in the table.
  • TableName: Gets or sets the name of the table.
  • PrimaryKey: Gets or sets an array of DataColumn objects that make up the primary key of the table.

Methods of C# DataTable in ADO.NET:

The DataTable class provides methods to perform various operations on the table and its data. Some important methods include:

  • NewRow(): Creates a new DataRow with the same schema as the DataTable.
  • Rows.Add(): Adds a new DataRow to the table.
  • Select(): Returns an array of DataRow objects that match the specified filter expression.
  • ImportRow(): Copies a DataRow into the DataTable.
  • AcceptChanges(): Commits all the changes made to the DataTable since the last call to AcceptChanges or RejectChanges.
  • RejectChanges(): Rolls back all the changes made to the DataTable since the last call to AcceptChanges or RejectChanges.

Steps to create a DataTable in C#:

To create a DataTable in C#, you can follow these steps:

  • Instantiate a new DataTable object.
  • Define the columns of the DataTable using the DataColumn class and add them to the DataTable's Columns collection.
  • Add rows to the DataTable by calling the NewRow() method to create a new DataRow object, populate it with data, and add it to the DataTable's Rows collection.
DataTable dataTable = new DataTable("YourTableName");
// Define columns
DataColumn column1 = new DataColumn("ID", typeof(int));
DataColumn column2 = new DataColumn("Name", typeof(string));
dataTable.Columns.Add(column1);
dataTable.Columns.Add(column2);
// Add rows
DataRow row1 = dataTable.NewRow();
row1["ID"] = 1;
row1["Name"] = "John Doe";
dataTable.Rows.Add(row1);
DataRow row2 = dataTable.NewRow();
row2["ID"] = 2;
row2["Name"] = "Jane Smith";
dataTable.Rows.Add(row2);

Iterating the DataTable in C#:

To iterate over the rows in a DataTable, you can use a foreach loop or a for loop. Here's an example using a foreach loop:

foreach (DataRow row in dataTable.Rows)
{
    int id = (int)row["ID"];
    string name = (string)row["Name"];
    Console.WriteLine($"ID: {id}, Name: {name}");
}

In the above code, we iterate over each DataRow object in the DataTable's Rows collection. We can access the data in each row using the column name or column index.

The DataTable class in ADO.NET provides a flexible and powerful way to work with tabular data in memory. It allows you to define the schema, manipulate data, and perform various operations on the data.