创建数据表Creating a DataTable

DataTable 表示一个内存内关系数据的表,可以独立创建和使用,也可以由其他 .NET Framework 对象使用,最常见的情况是作为 DataSet 的成员使用。A DataTable, which represents one table of in-memory relational data, can be created and used independently, or can be used by other .NET Framework objects, most commonly as a member of a DataSet.

您可以使用相应的datatable构造函数创建datatable对象。You can create a DataTable object by using the appropriate DataTable constructor. 您可以通过使用add方法将其添加到DataTable对象的Tables集合中,将其添加到数据集You can add it to the DataSet by using the Add method to add it to the DataTable object's Tables collection.

您还可以通过使用DataAdapter对象的FillFillSchema方法,或使用ReadXml的预定义或推断 XML 架构在数据集中创建DataTable对象ReadXmlSchema 数据集InferXmlSchema方法。You can also create DataTable objects within a DataSet by using the Fill or FillSchema methods of the DataAdapter object, or from a predefined or inferred XML schema using the ReadXml, ReadXmlSchema, or InferXmlSchema methods of the DataSet. 请注意,将DataTable添加为一个数据集tables集合的成员后,不能将其添加到任何其他数据集的表的集合中。Note that after you have added a DataTable as a member of the Tables collection of one DataSet, you cannot add it to the collection of tables of any other DataSet.

首次创建DataTable时,它没有架构(即结构)。When you first create a DataTable, it does not have a schema (that is, a structure). 若要定义表的架构,必须创建对象并将其DataColumn添加到表的Columns集合中。To define the schema of the table, you must create and add DataColumn objects to the Columns collection of the table. 您还可以为表定义主键列,并创建约束对象并将其添加到表的约束集合。You can also define a primary key column for the table, and create and add Constraint objects to the Constraints collection of the table. DataTable定义了架构之后,可以通过将DataRow对象添加到表的rows集合中来向表中添加数据行。After you have defined the schema for a DataTable, you can add rows of data to the table by adding DataRow objects to the Rows collection of the table.

创建TableName DataTable时,不需要提供属性的值; 您可以在其他时间指定该属性,也可以将其留空。You are not required to supply a value for the TableName property when you create a DataTable; you can specify the property at another time, or you can leave it empty. 但是,当您将没有TableName值的表添加到数据集时,将为该表给定增量默认名称表N,并以 "table" 作为 Table0。However, when you add a table without a TableName value to a DataSet, the table will be given an incremental default name of TableN, starting with "Table" for Table0.

备注

当你提供TableName值时,我们建议你避免使用 "TableN" 命名约定,因为所提供的名称可能与数据集中的现有默认表名称冲突。We recommend that you avoid the "TableN" naming convention when you supply a TableName value, because the name you supply may conflict with an existing default table name in the DataSet. 如果提供的名称已经存在,将引发异常。If the supplied name already exists, an exception is thrown.

下面的示例创建DataTable对象的一个实例,并为其分配名称 "Customers"。The following example creates an instance of a DataTable object and assigns it the name "Customers."

Dim workTable as DataTable = New DataTable("Customers")  
DataTable workTable = new DataTable("Customers");  

下面的示例通过将 DataTable 添加到DataSetTables集合来创建DataTable的实例。The following example creates an instance of a DataTable by adding it to the Tables collection of a DataSet.

Dim customers As DataSet = New DataSet  
Dim customersTable As DataTable = _  
   customers.Tables.Add("CustomersTable")  
DataSet customers = new DataSet();  
DataTable customersTable = customers.Tables.Add("CustomersTable");  

请参阅See also