7.1 Creating a DataTable

There are a number of ways a DataTable can be created. In the simplest case, a DataTable is created using the new keyword. The constructor accepts an optional argument that allows the TableName property to be set. If the TableName argument isn't supplied, the default name of the table is Table; subsequent tables are named Table1, Table2, and so on. The following example demonstrates how to create a table named MyTable:

DataTable dt = new DataTable("MyTable");

A DataTable can also be created automatically in a DataSet when the Fill( ) or FillSchema( ) method of the DataAdapter is called specifying a table that doesn't already exist in the DataSet. The DataAdapter class and the Fill( ) and FillSchema( ) methods are discussed in detail in Chapter 14.

DataTable objects can also be created from other DataTable objects. A new DataTable can be created from either the table schema alone, the schema and the data, or a subset of the DataTable based on if and how the data has been modified. Methods to do this are discussed later in this chapter.

    Part I: ADO.NET Tutorial
    Part II: ADO.NET Core Classes
    Part III: API Quick Reference