DataTableCollection.Contains 方法

定义

获取一个值,该值指示集合中是否存在具有指定名称的 DataTable 对象。Gets a value indicating whether a DataTable object with the specified name exists in the collection.

重载

Contains(String)

获取一个值,该值指示集合中是否存在具有指定名称的 DataTable 对象。Gets a value that indicates whether a DataTable object with the specified name exists in the collection.

Contains(String, String)

获取一个值,该值指示集合中是否存在具有指定名称和表命名空间的 DataTable 对象。Gets a value that indicates whether a DataTable object with the specified name and table namespace exists in the collection.

Contains(String)

获取一个值,该值指示集合中是否存在具有指定名称的 DataTable 对象。Gets a value that indicates whether a DataTable object with the specified name exists in the collection.

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

要查找的 DataTable 的名称。The name of the DataTable to find.

返回

如果存在指定的表,则为 true;否则为 falsetrue if the specified table exists; otherwise false.

示例

下面的示例测试中DataTableCollection是否存在名为 "供应商" 的表。The following example tests whether a table with the name "Suppliers" exists in the DataTableCollection.

private void TestForTableName()
{
    // Get the DataSet of a DataGrid.
    DataSet thisDataSet = (DataSet)DataGrid1.DataSource;

    // Get the DataTableCollection through the Tables property.
    DataTableCollection tablesCol = thisDataSet.Tables;

    // Check if the named table exists.
    if (tablesCol.Contains("Suppliers")) 
        Console.WriteLine("Table named Suppliers exists");
}
Private Sub TestForTableName()
   ' Get the DataSet of a DataGrid.
   Dim thisDataSet As DataSet = CType(DataGrid1.DataSource, DataSet)

   ' Get the DataTableCollection through the Tables property.
   Dim tablesCol As DataTableCollection = thisDataSet.Tables

   ' Check if the named table exists.
   If tablesCol.Contains("Suppliers") Then 
      Console.WriteLine("Table named Suppliers exists")
   End If
End Sub

注解

使用属性指定DataTable对象的名称。TableNameYou specify the name of the DataTable object by using the TableName property. DataTable 如果Add使用方法将添加到集合中,不传递任何参数,则将根据添加表的顺序为该表提供默认名称("Table1"、"Table2" 等)。If you add a DataTable to the collection with the Add method, passing no arguments, the table is given a default name, based on the order in which the table was added ("Table1", "Table2", and so on).

若要获取的索引DataTable,请IndexOf使用方法。To get the index of a DataTable, use the IndexOf method.

备注

false两个或多个表具有相同名称但命名空间不同时返回。Returns false when two or more tables have the same name but different namespaces. 如果在将表名只匹配到一个表时存在任何二义性,则该调用不会成功。The call does not succeed if there is any ambiguity when matching a table name to exactly one table.

另请参阅

Contains(String, String)

获取一个值,该值指示集合中是否存在具有指定名称和表命名空间的 DataTable 对象。Gets a value that indicates whether a DataTable object with the specified name and table namespace exists in the collection.

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

参数

name
String

要查找的 DataTable 的名称。The name of the DataTable to find.

tableNamespace
String

作为查找范围的 DataTable 命名空间的名称。The name of the DataTable namespace to look in.

返回

如果存在指定的表,则为 true;否则为 falsetrue if the specified table exists; otherwise false.

示例

下面的示例测试中DataTableCollection是否存在名为 "供应商" 的表。The following example tests whether a table with the name "Suppliers" exists in the DataTableCollection.

private void TestForTableName()
{
    // Get the DataSet of a DataGrid.
    DataSet thisDataSet = (DataSet)DataGrid1.DataSource;

    // Get the DataTableCollection through the Tables property.
    DataTableCollection tablesCol = thisDataSet.Tables;

    // Check if the named table exists.
    if (tablesCol.Contains("Suppliers")) 
        Console.WriteLine("Table named Suppliers exists");
}
Private Sub TestForTableName()
   ' Get the DataSet of a DataGrid.
   Dim thisDataSet As DataSet = CType(DataGrid1.DataSource, DataSet)

   ' Get the DataTableCollection through the Tables property.
   Dim tablesCol As DataTableCollection = thisDataSet.Tables

   ' Check if the named table exists.
   If tablesCol.Contains("Suppliers") Then 
      Console.WriteLine("Table named Suppliers exists")
   End If
End Sub

注解

使用属性指定DataTable对象的名称。TableNameYou specify the name of the DataTable object by using the TableName property. DataTable 如果Add使用方法将添加到集合中,不传递任何参数,则将根据添加表的顺序为该表提供默认名称("Table1"、"Table2" 等)。If you add a DataTable to the collection with the Add method, passing no arguments, the table is given a default name, based on the order in which the table was added ("Table1", "Table2", and so on).

备注

false两个或多个表具有相同名称但命名空间不同时返回。Returns false when two or more tables have the same name but different namespaces. 如果在将表名只匹配到一个表时存在任何二义性,则该调用不会成功。The call does not succeed if there is any ambiguity when matching a table name to exactly one table.

若要获取的索引DataTable,请IndexOf使用方法。To get the index of a DataTable, use the IndexOf method.

另请参阅

适用于