DataColumnCollection.Contains(String) 方法

定義

檢查集合是否包含具有指定名稱的資料行。Checks whether the collection contains a column with the specified name.

public:
 bool Contains(System::String ^ name);
public bool Contains (string name);
member this.Contains : string -> bool
Public Function Contains (name As String) As Boolean

參數

name
String

要尋找的資料行 ColumnNameThe ColumnName of the column to look for.

傳回

如果存在具有這個名稱的資料行,則為 true,否則為 falsetrue if a column exists with this name; otherwise, false.

範例

下列範例會使用Contains方法CanRemove和方法,來判斷是否可以移除資料行。The following example uses the Contains method and the CanRemove method to determine whether the column can be removed. 若是如此,則會移除該資料行。If so, the column is removed.

   private void RemoveColumn(string columnName, DataTable table)
   {
       DataColumnCollection columns = table.Columns;

       if (columns.Contains(columnName))
           if (columns.CanRemove(columns[columnName]))
               columns.Remove(columnName);
   }
Private Sub RemoveColumn(columnName As String, table As DataTable)
    Dim columns As DataColumnCollection = table.Columns

    If columns.Contains(columnName) Then 
        If columns.CanRemove(columns(columnName)) Then 
            columns.Remove(columnName)
        End If
    End If
End Sub

備註

在資料行上執行其他作業之前,請使用方法來確認資料行是否存在。ContainsUse the Contains method to confirm the existence of a column before you perform additional operations on the column. false當兩個或多個資料行具有相同的名稱,但命名空間不同時,此方法會傳回。The method returns false when two or more columns have the same name but different namespaces. 如果將資料行名稱比對成單一資料行,則呼叫不會成功。The call does not succeed if there is any ambiguity when matching a column name to exactly one column.

適用於

另請參閱