向数据表添加列Adding Columns to a DataTable

包含由表的Columns属性引用的对象的DataColumn集合。 DataTableA DataTable contains a collection of DataColumn objects referenced by the Columns property of the table. 这个列的集合与任何约束一起定义表的架构(即结构)。This collection of columns, along with any constraints, defines the schema, or structure, of the table.

您可以使用DataColumn构造函数在表中创建DataColumn对象,或调用表的Columns属性的DataColumnCollection Add方法(即)。You create DataColumn objects within a table by using the DataColumn constructor, or by calling the Add method of the Columns property of the table, which is a DataColumnCollection. Add方法接受可选的ColumnNameDataTypeExpression参数,并创建新的DataColumn作为集合的成员。The Add method accepts optional ColumnName, DataType, and Expression arguments and creates a new DataColumn as a member of the collection. 它还接受现有DataColumn对象并将其添加到集合中,并返回对添加的datacolumn的引用(如果请求)。It also accepts an existing DataColumn object and adds it to the collection, and returns a reference to the added DataColumn if requested. 因为DataTable对象不特定于任何数据源,所以在指定DataColumn的数据类型时,将使用 .NET Framework 类型。Because DataTable objects are not specific to any data source, .NET Framework types are used when specifying the data type of a DataColumn.

下面的示例将四列添加到DataTableThe following example adds four columns to a DataTable.

Dim workTable As DataTable = New DataTable("Customers")  
  
Dim workCol As DataColumn = workTable.Columns.Add( _  
    "CustID", Type.GetType("System.Int32"))  
workCol.AllowDBNull = false  
workCol.Unique = true  
  
workTable.Columns.Add("CustLName", Type.GetType("System.String"))  
workTable.Columns.Add("CustFName", Type.GetType("System.String"))  
workTable.Columns.Add("Purchases", Type.GetType("System.Double"))  
DataTable workTable = new DataTable("Customers");  
  
DataColumn workCol = workTable.Columns.Add("CustID", typeof(Int32));  
workCol.AllowDBNull = false;  
workCol.Unique = true;  
  
workTable.Columns.Add("CustLName", typeof(String));  
workTable.Columns.Add("CustFName", typeof(String));  
workTable.Columns.Add("Purchases", typeof(Double));  

在此示例中,请注意, CustID列的属性设置为不允许DBNull值并将值限制为唯一。In the example, notice that the properties for the CustID column are set to not allow DBNull values and to constrain values to be unique. 但是,如果将CustID列定义为表的主键列,则AllowDBNull属性将自动设置为false ,并且Unique属性将自动设置为trueHowever, if you define the CustID column as the primary key column of the table, the AllowDBNull property will automatically be set to false and the Unique property will automatically be set to true. 有关详细信息,请参阅定义主键For more information, see Defining Primary Keys.

注意

如果没有为列提供列名称,则在将列添加到DataColumnCollection时,将为该列指定递增的默认名称 以 "Column1" 开头)。If a column name is not supplied for a column, the column is given an incremental default name of ColumnN, starting with "Column1", when it is added to the DataColumnCollection. 建议在提供列名时避免使用 "ColumnN" 命名约定,因为所提供的名称可能与DataColumnCollection中现有的默认列名冲突。We recommend that you avoid the naming convention of "ColumnN" when you supply a column name, because the name you supply may conflict with an existing default column name in the DataColumnCollection. 如果提供的名称已经存在,将引发异常。If the supplied name already exists, an exception is thrown.

如果将 XElement 用作 DataType 中的 DataColumnDataTable,则在读入数据时,XML 序列化将不起作用。If you are using XElement as the DataType of a DataColumn in the DataTable, XML serialization will not work when you read in data. 例如,如果通过使用 XmlDocument 方法来写出 DataTable.WriteXml,则在序列化为 XML 时,XElement 中会出现一个额外的父节点。For example, if you write out a XmlDocument by using the DataTable.WriteXml method, upon serialization to XML there is an additional parent node in the XElement. 若要解决此问题,请使用 SqlXml 类型来代替 XElementTo work around this problem, use the SqlXml type instead of XElement. ReadXmlWriteXmlSqlXml 均能正确使用。ReadXml and WriteXml work correctly with SqlXml.

请参阅See also