將資料行加入至 DataTableAdding 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的函式,或呼叫資料表之Columns屬性的DataColumnCollection Add方法(也就是),在資料表中建立DataColumn物件。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. 當您提供資料行名稱時,建議您避免「資料行N」的命名慣例,因為您所提供的名稱可能會與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 當做 DataTypeDataColumnDataTable,當您讀入資料時,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. ReadXmlWriteXml 會搭配 SqlXml 正確運作。ReadXml and WriteXml work correctly with SqlXml.

另請參閱See also