System.Data Namespace
Represents the schema of a column in a DataTable.
Visibility |
Name |
Value Type |
Accessibility |
public |
AllowDBNull
|
Boolean |
[ Get , Set ] |
public |
AutoIncrement
|
Boolean |
[ Get , Set ] |
public |
AutoIncrementSeed
|
Int64 |
[ Get , Set ] |
public |
AutoIncrementStep
|
Int64 |
[ Get , Set ] |
public |
Caption
|
String |
[ Get , Set ] |
public |
ColumnMapping
|
MappingType |
[ Get , Set ] |
public |
ColumnName
|
String |
[ Get , Set ] |
public |
DataType
|
Type |
[ Get , Set ] |
public |
DateTimeMode
|
DataSetDateTime |
[ Get , Set ] |
public |
DefaultValue
|
Object |
[ Get , Set ] |
public |
Expression
|
String |
[ Get , Set ] |
public |
ExtendedProperties
|
PropertyCollection |
[ Get ] |
public |
MaxLength
|
Int32 |
[ Get , Set ] |
public |
Namespace
|
String |
[ Get , Set ] |
public |
Ordinal
|
Int32 |
[ Get ] |
public |
Prefix
|
String |
[ Get , Set ] |
public |
ReadOnly
|
Boolean |
[ Get , Set ] |
public |
Table
|
DataTable |
[ Get ] |
public |
Unique
|
Boolean |
[ Get , Set ] |
|
The DataColumn is the fundamental building block for creating the schema of a DataTable. You build the schema by adding one or more DataColumn objects to the DataColumnCollection. For more information, see Adding DataColumns to a Table.
Each DataColumn has a DataType property that determines the kind of data the each DataColumn contains. For example, you can restrict the data type to integers, or strings, or decimals. Because data contained by the DataTable is usually merged back into its original data source, you must match the data types to those in the data source. For more information, see Mapping .NET Data Provider Data Types to .NET Framework Data Types.
Properties such as AllowDBNull, Unique, and ReadOnly place restrictions on the entry and updating of data, thereby helping to ensure data integrity. You can also use the AutoIncrement, AutoIncrementSeed, and AutoIncrementStep properties to control automatic data generation. For more information about AutoIncrement columns, see Creating AutoIncrement Columns. For more information, see Defining a Primary Key for a Table.
You can also ensure that values in a DataColumn are unique by creating a UniqueConstraint and adding it to the ConstraintCollection of the DataTable to which the DataColumn belongs. For more information, see Adding Constraints to a Table.
To create a relation between DataColumn objects, create a DataRelation object and add it to the DataRelationCollection of a DataSet. When you add a DataRelation to the collection, a ForeignKeyConstraint, which also helps ensure data integrity, is created automatically.
You can use the Expression property of the DataColumn object filter rows, calculate the values in a column, or create an aggregate column. For more information, see Creating Expression Columns.
The following example initializes a DataTable with several DataColumn objects.
private void MakeTable ( DataTable myTable ) {
// create a DataTable.
DataTable myTable = new DataTable ( "myTable" );
// create a DataColumn and set various properties.
DataColumn myColumn = new DataColumn ( );
myColumn.DataType = System.Type.GetType ( "System.Decimal" );
myColumn.AllowDBNull = false;
myColumn.Caption = "Price";
myColumn.ColumnName = "Price";
myColumn.DefaultValue = 25;
// add the column to the table.
myTable.Columns.Add ( myColumn );
// add 10 rows and set values.
DataRow myRow;
for ( int i = 0; i < 10; i++ ) {
myRow = myTable.NewRow ( );
myRow [ "Price" ] = i + 1;
// be sure to add the new row to the DataRowCollection.
myTable.Rows.Add ( myRow );
}
}
Private Sub MakeTable ( )
' create a DataTable.
Dim myTable As DataTable = new DataTable ( "myTable" )
' create a DataColumn and set various properties.
Dim myColumn As DataColumn = New DataColumn
myColumn.DataType = System.Type.GetType ( "System.Decimal" )
myColumn.AllowDBNull = False
myColumn.Caption = "Price"
myColumn.ColumnName = "Price"
myColumn.DefaultValue = 25
' add the column to the table.
myTable.Columns.Add ( myColumn )
' add 10 rows and set values.
Dim myRow As DataRow
Dim i As Integer
For i = 0 to 9
myRow = myTable.NewRow ( )
myRow ( "Price" ) = i + 1
' be sure to add the new row to the DataRowCollection.
myTable.Rows.Add ( myRow )
Next i
End Sub |
|
C# |
VB |
Add DataColumnCollection Constraints ConstraintCollection DataRow DataTable DataSet NewRow DataRowCollection UniqueConstraint