DataColumn Class (ADO.NET)
The DataColumn
class represents a column in a DataTable
. It defines the schema of the column, including its name, data type, constraints, and default value.
Overview
A DataTable
is a collection of DataColumn
objects and DataRow
objects. Each DataColumn
defines the properties of a single column within the table. This includes:
- ColumnName: A unique string identifier for the column.
- DataType: The type of data the column can store (e.g.,
string
,int
,DateTime
). - AllowDBNull: A boolean value indicating whether the column can contain
null
values. - DefaultValue: The default value for new rows in this column.
- MaxLength: The maximum length for string-based data types.
- Unique: A boolean value indicating whether all values in the column must be unique.
- Expression: A string defining an expression to calculate the column's value or filter rows.
Creating a DataColumn
You can create a DataColumn
programmatically or add it to a DataTable
through its Columns
collection.
Programmatic Creation
You can instantiate a DataColumn
using its constructors. The most common constructor takes the column name as an argument.
// Create a DataColumn for employee ID
DataColumn idColumn = new DataColumn("EmployeeID", typeof(int));
idColumn.Unique = true;
idColumn.AllowDBNull = false;
idColumn.AutoIncrement = true;
idColumn.AutoIncrementSeed = 1;
idColumn.AutoIncrementStep = 1;
// Create a DataColumn for employee name
DataColumn nameColumn = new DataColumn("FirstName", typeof(string));
nameColumn.AllowDBNull = false;
nameColumn.MaxLength = 50;
// Create a DataColumn for hire date
DataColumn hireDateColumn = new DataColumn("HireDate", typeof(DateTime));
hireDateColumn.DefaultValue = DateTime.Now;
Adding to a DataTable
Once created, a DataColumn
can be added to the Columns
collection of a DataTable
.
DataTable employeesTable = new DataTable("Employees");
// Add the columns created above
employeesTable.Columns.Add(idColumn);
employeesTable.Columns.Add(nameColumn);
employeesTable.Columns.Add(hireDateColumn);
DataColumn Properties in Detail
DataType
This property is crucial as it dictates the kind of data that can be stored in the column and affects how data is compared and processed. Common types include:
typeof(int)
typeof(string)
typeof(DateTime)
typeof(decimal)
typeof(bool)
typeof(byte[])
AllowDBNull
Setting this to false
enforces that every row must have a value for this column. If you try to add a row with a null
value to a column where AllowDBNull
is false
, a ConstraintException
will be thrown.
DefaultValue
When a new row is added to the DataTable
, if a value is not explicitly provided for a column, the DefaultValue
of that column will be used.
Unique
If set to true
, this property ensures that no two rows in the DataTable
can have the same value in this column. A ConstraintException
is thrown if a duplicate value is attempted.
Expression
The Expression
property is a powerful feature that allows you to perform calculations or filtering directly within the DataTable
. It supports a syntax similar to SQL's WHERE and SELECT clauses.
Example: Using Expression for Calculation
Define a calculated column for the full name:
DataColumn fullNameColumn = new DataColumn("FullName", typeof(string), "FirstName + ' ' + LastName");
employeesTable.Columns.Add(fullNameColumn);
Filter rows using an expression:
DataRow[] filteredRows = employeesTable.Select("EmployeeID > 100");
Constraints
In addition to the properties like Unique
and AllowDBNull
, you can define more complex constraints on DataColumn
objects using the Constraints
collection of the DataTable
. Common constraints include:
UniqueConstraint
: Ensures all values in a column (or set of columns) are unique.ForeignKeyConstraint
: Establishes relationships between tables.UniqueConstraint
: Ensures all values in a column (or set of columns) are unique.