天天看点

DataTable.Clear 方法

清除所有数据的 DataTable。

[Visual Basic]
         Public Sub Clear()                [C#]
         public                void         Clear();                
[C++]
         public:                void         Clear();                
[JScript]
         public function Clear();           

备注

所有表中的所有行都被移除。如果表中有任何将使子行受到影响的强制子关系,则生成异常。

如果 DataSet 绑定到 XmlDataDocument,则调用 DataSet.Clear 或

DataTable.Clear

将引发 NotSupportedException。为避免这种情况,请遍历每个表,逐个移除每一行。

示例

[Visual Basic, C#, C++] 以下示例清除所有数据的表。

[Visual Basic] 
Private Sub ClearTable(myTable As DataTable)
    Try
        myTable.Clear()
    Catch e As DataException
    ' Process exception and return.
         Dim log As System.Diagnostics.EventLog = New System.Diagnostics.EventLog()
         log.Source = "My Application"
         log.WriteEntry(e.ToString())
         Console.WriteLine("Exception of type {0} occurred.", e.GetType().ToString())
    End Try
End Sub

[C#] 
private void ClearTable(DataTable myTable){
    try{
       myTable.Clear();
    }
    catch (DataException e){
    // Process exception and return.
        System.Diagnostics.EventLog log = new System.Diagnostics.EventLog();
        log.Source = "My Application";
        log.WriteEntry(e.ToString());
        Console.WriteLine("Exception of type {0} occurred.", e.GetType());
    }
    
 }

[C++] 
private:
void ClearTable(DataTable* myTable){
    try{
       myTable->Clear();
    }
    catch (DataException* e){
    // Process exception and return.
        System::Diagnostics::EventLog* log = new System::Diagnostics::EventLog();
        log->Source = S"My Application";
        log->WriteEntry(e->ToString());
        Console::WriteLine(S"Exception of type {0} occurred.", e->GetType());
    }
    
 }
      

[JScript] 没有可用于 JScript 的示例。若要查看 Visual Basic、C# 或 C++ 示例,请单击页左上角的“语言筛选器”按钮

DataTable.Clear 方法

要求

平台: 

Windows 98, Windows NT 4.0, Windows ME, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 系列, .NET Framework 精简版

请参见

DataTable 类 | DataTable 成员 | System.Data 命名空间