DataTable.Clear 方法

定义

清除所有数据的 DataTableClears the DataTable of all data.

public:
 void Clear();
public void Clear ();
member this.Clear : unit -> unit
Public Sub Clear ()

示例

下面的示例将清除所有数据的表。The following example clears the table of all data.

private void ClearTable(DataTable table)
{
    try
    {
        table.Clear();
    }
    catch (DataException e)
    {
        // Process exception and return.
        Console.WriteLine("Exception of type {0} occurred.", 
            e.GetType());
    }
}
Private Sub ClearTable(table As DataTable)
    Try
        table.Clear()
    Catch e As DataException
    ' Process exception and return.
         Console.WriteLine("Exception of type {0} occurred.", _
           e.GetType().ToString())
    End Try
End Sub

注解

所有表中的所有行都将被删除。All rows in all tables are removed. 如果表具有将导致子行孤立的任何已强制的子关系,则会生成异常。An exception is generated if the table has any enforced child relations that would cause child rows to be orphaned.

如果 DataSet 绑定到 XmlDataDocument,则调用 DataSet.ClearDataTable.Clear 将引发 NotSupportedExceptionIf the DataSet is bound to an XmlDataDocument, calling DataSet.Clear or DataTable.Clear raises the NotSupportedException. 若要避免这种情况,请遍历每个表,一次删除一个行。To avoid this situation, traverse each table, removing each row one at a time. 使用 NewRow 创建新行时,必须先处理这些行,然后才能调用 ClearWhen you use NewRow to create new rows, the rows must be processed before you call Clear.

适用于

另请参阅