DataTable, DataColumn and DataRow classes



A DataTable object represents a database table. A data table is a collection of columns and rows. The DataRow object represents a table row, and the DataColumn object represents a column of the table. The first step to working with these three objects is to create a data table schema, which is defined by the DataColumnCollection object. You use the Add method of the DataColumnCollection to add columns to the collections. The Columns property of the DataTable object represents the DataColumnCollection, which is a collection of DataColumn objects in a DataTable. You use a DataRow object to add data to a data table. TheDataRowCollection object represents a collection of rows of a DataTable object, which can be accessed by its Rows property.

Figure shows the relationship between the DataTable, DataRow, and DataColumn.

Figure-5.6.gif

Figure: Relationship between the DataTable, the DataRow, and the DataColumn.

To understanding a data table schema, take a look at below figure shows the schema of the Customers table of Northwind database.

Figure-5.7.jpg

Figure: The Customers database table schema

The Field Name column represents the name of the field, the data type column represents the type of data you can store in that column, and the Description column is for designers to explain a field.

The Field properties area represents the properties of a column. If you look at the General tab, you'll notice Field size, Caption, Allow Zero Length, Default Value, Validation rule, Indexed fields, and others. These options are column properties. All of these properties are pretty self-explanatory. For example, field size means the number of characters you can store in a column, Allow zero size Length indicates whether a column can have zero length values and so on.

Note: A database table column is also called a field.

So, when I talk about creating a data table, I mean I build a database table (in memory, of course) and its schema using the DataTable and the DataColumn objects. Based on a column requirement, you can also apply constrains on column. A constraint is a rule that restricts adding unwanted data to a column. For example, the uniqueness of a column means that a column can't have duplicate values. You'll see constraints in more detail under the "The Data Column" section.

After creating a data table schema, the second step is to add data rows to the data table. You use data rows using the DataRow object. After adding data to a database table, the field table looks like figure below.

Figure-5.8.jpg

Figure: Filled data view of the Customers table

As you can see from above figure, the data table has data based on its column properties.

Conclusion

Hope this article would have helped you in understanding DataTable, DataColum and DataRow classes in ADO.NET. See my other articles on the website on ADO.NET.


Similar Articles
Mindcracker
Founded in 2003, Mindcracker is the authority in custom software development and innovation. We put best practices into action. We deliver solutions based on consumer and industry analysis.