DataTable.Clear 方法

定義

清除所有資料的 DataTable

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

範例

下列範例會清除所有數據的數據表。

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

備註

所有數據表中的所有數據列都會移除。 如果數據表具有任何強制的子關聯,導致子數據列被遺棄,就會產生例外狀況。

DataSet如果 系結至 XmlDataDocument,則呼叫 DataSet.ClearDataTable.Clear 引發 NotSupportedException。 若要避免這種情況,請周遊每個數據表,一次移除一個數據列。 當您使用 NewRow 建立新資料列時,必須先處理資料列,才能呼叫 Clear

適用於

另請參閱