DataTable Objects
The DataTable object is a core component of ADO.NET, representing an in-memory table of data. It is used to store and manipulate data retrieved from a data source, independent of the data source itself.
Key Features of DataTable
- Schema Definition: A
DataTabledefines its schema throughColumnsandConstraints. - Data Storage: It stores data in rows, where each row contains values for the defined columns.
- Data Manipulation: Supports adding, deleting, and modifying rows, as well as filtering and sorting data.
- Relationships: Can participate in relationships with other
DataTableobjects within aDataSet. - Versioning: Supports row versioning (
Current,Original,Proposed) for tracking changes.
Creating and Populating a DataTable
You can create a DataTable programmatically or have it created by a DataAdapter.
Here's a simple example of creating a DataTable and adding columns and rows:
// C# Example
DataTable productsTable = new DataTable("Products");
// Define columns
DataColumn idColumn = new DataColumn("ProductID", typeof(int));
idColumn.AutoIncrement = true;
idColumn.AutoIncrementSeed = 1;
productsTable.Columns.Add(idColumn);
DataColumn nameColumn = new DataColumn("ProductName", typeof(string));
nameColumn.MaxLength = 50;
productsTable.Columns.Add(nameColumn);
DataColumn priceColumn = new DataColumn("Price", typeof(decimal));
productsTable.Columns.Add(priceColumn);
// Add constraints (optional)
productsTable.Constraints.Add(new UniqueConstraint("PK_ProductID", idColumn));
// Add rows
DataRow row1 = productsTable.NewRow();
row1["ProductName"] = "Laptop";
row1["Price"] = 1200.50m;
productsTable.Rows.Add(row1);
DataRow row2 = productsTable.NewRow();
row2["ProductName"] = "Keyboard";
row2["Price"] = 75.00m;
productsTable.Rows.Add(row2);
// Access data
foreach (DataRow row in productsTable.Rows)
{
Console.WriteLine($"ID: {row["ProductID"]}, Name: {row["ProductName"]}, Price: {row["Price"]}");
}
DataTable Schema Elements
Columns
DataColumn objects define the structure of the DataTable. Each column has a name, a data type, and can have properties like AllowDBNull, AutoIncrement, and MaxLength.
Constraints
Constraint objects enforce rules on the data within a DataTable. Common constraints include:
UniqueConstraint: Ensures that all values in a specified column or set of columns are unique.ForeignKeyConstraint: Defines a relationship between two tables, ensuring referential integrity.
DataTable provides a powerful abstraction for working with structured data in memory, making it a cornerstone for disconnected data access scenarios in ADO.NET.
Working with Rows
DataRow objects represent a single record within a DataTable. You can create new rows using NewRow() and add them to the table's Rows collection.
You can access, modify, and delete rows. When modifying a row, ADO.NET tracks the changes, allowing you to accept or reject them.
Filtering and Searching
DataTable offers efficient ways to find specific rows:
Select()method: Returns an array ofDataRowobjects that match a specified filter expression.Find()method: Quickly finds a row based on its primary key values.
Example using Select():
// Find products with price greater than 100
DataRow[] expensiveProducts = productsTable.Select("Price > 100");
foreach (DataRow row in expensiveProducts)
{
Console.WriteLine($"Expensive Product: {row["ProductName"]}");
}
DataTable and DataSet
A DataTable is often used within a DataSet. A DataSet can contain multiple DataTable objects, along with relationships between them, allowing you to model complex data structures.
DataTable is crucial for efficient data management in ADO.NET applications, especially when dealing with large amounts of data or disconnected architectures.