CHAPTER 22 ADO.NET PART II: THE DISCONNECTED LAYER
Working with DataColumns
The DataColumn type represents a single column within a DataTable. Collectively speaking, the set of all
DataColumn types bound to a given DataTable represents the foundation of a table’s schema information.
For example, if you were to model the Inventory table of the AutoLot database (see Chapter 21), you
would create four DataColumns, one for each column (CarID, Make, Color, and PetName). After you create
your DataColumn objects, you typically add them into the columns collection of the DataTable type (using
the Columns property).
Based on your background, you might know that you can assign a given column in a database table
a set of constraints (e.g., configured as a primary key, assigned a default value, or configured to contain
read-only information). Also, every column in a table must map to an underlying data type. For example,
the Inventory table’s schema requires that the CarID column map to an integer, while Make, Color, an d
PetName map to an array of characters. The DataColumn class has numerous properties that allow you to
configure precisely these things. Table 22-3 provides a rundown of some core properties.
Table 22-3. Properties of the DataColumn
Properties
Meaning in Life
AllowDBNull
You use this property to indicate whether a row can specify null values
in this column. The default value is true.
AutoIncrement
AutoIncrementSeed
AutoIncrementStep
864
You use these properties to configure the autoincrement behavior for a
given column. This can be helpful when you want to ensure unique
values in a given DataColumn (such as a primary key). By default, a
DataColumn does not support autoincrement behavior.
Caption
This property gets or sets the caption you want to display for this
column. This allows you to define a user-friendly version of a literal
database column name.
ColumnMapping
This property determines how a DataColumn is represented when a
DataSet is saved as an XML document using the DataSet.WriteXml()
method. You can specify that the data column should be written out as
an XML element, an XML attribute, simple text content, or ignored
altogether.
ColumnName
This property gets or sets the name of the column in the Columns
collection (meaning how it is represented internally by the DataTable).
If you do not set the ColumnName explicitly, the default values are Column
with (n+1) numerical suffixes (e.g., Column1, Column2, and Column3).
DataType
This property defines the data type (e.g., Boolean, string, or float)
stored in the column.
DefaultValue
This property gets or sets the default value assigned to this column
when you insert new rows.