Visual Basic .NET - ADO .Net - DataTable Class

The DataTable class in VB.NET is a central component of the ADO.NET framework and represents a table of data in memory. It provides a structured way to store, manipulate, and query data independently of a data source, such as a database.

Structure: A DataTable consists of rows and columns that hold the actual data. You can define columns and their data types using DataColumn objects and add rows to the DataTable to store the data.

Columns: The DataColumnCollection property of the DataTable provides access to the collection of columns. You can add, remove, and modify columns dynamically. Each column is represented by a DataColumn object, which specifies its name, data type, constraints, and other properties.

Rows: The Rows property of the DataTable provides access to the collection of rows. You can add, remove, and modify rows in the DataTable. Each row is represented by a DataRow object, which holds the data for each column.

Data Retrieval: You can access the data in a DataTable by iterating over the rows and columns or by using indexes. You can retrieve the value of a specific cell using the row and column index or by specifying the column name.

Constraints: The DataTable supports various constraints, such as primary key constraints, unique constraints, and foreign key constraints. Constraints help maintain data integrity and enforce rules on the data in the DataTable.

Data Modification: You can modify the data in a DataTable by adding, updating, or deleting rows and changing the values of cells. The changes made in the DataTable can be persisted to a data source using a DataAdapter.

Data Binding: The DataTable can be used as a data source for data-bound controls, such as grids or lists. You can bind controls directly to the DataTable, allowing for easy display and manipulation of data.

Here's an example that demonstrates the usage of the DataTable class:

Imports System.Data
Module Program
    Sub Main()
        ' Create a new DataTable
        Dim dataTable As New DataTable("Customers")
        ' Add columns to the DataTable
        dataTable.Columns.Add("ID", GetType(Integer))
        dataTable.Columns.Add("Name", GetType(String))
        dataTable.Columns.Add("Email", GetType(String))
        ' Add rows to the DataTable
        dataTable.Rows.Add(1, "John Doe", "[email protected]")
        dataTable.Rows.Add(2, "Jane Smith", "[email protected]")
        ' Access and modify data in the DataTable
        For Each row As DataRow In dataTable.Rows
            row("Email") = "[email protected]"
        Next
        ' Retrieve data from the DataTable
        For Each row As DataRow In dataTable.Rows
            Dim id As Integer = CInt(row("ID"))
            Dim name As String = CStr(row("Name"))
            Dim email As String = CStr(row("Email"))
            Console.WriteLine("ID: " & id)
            Console.WriteLine("Name: " & name)
            Console.WriteLine("Email: " & email)
            Console.WriteLine()
        Next
    End Sub
End Module

In the example, a new DataTable named "Customers" is created with three columns: "ID", "Name", and "Email". Rows are added to the DataTable with sample data. The data in the "Email" column is modified for each row. Finally, the data is retrieved from the DataTable by iterating over the rows and columns.