Delete All row from DataTable in DataSet

2019-08-15 10:29发布

问题:

I'd like to erase all row in a datatable located in a dataset and then update the DataBase using the method Update() of TableAdapter.

The insert works fine, I put it so you can see what I'm doing. I tried the method Clear() for datatable which was supposed to erase all row but at the moment it does nothing.

I would like to use a kind of SQL query like DELETE * FROM demande; but I dont get how to do it

protected void InsertDemande_Click(object sender, EventArgs e)
{
    //TB = textBox
    //Parsing date (5th row of my dataTable is type date)
    string[] tabdate = TB_date_demande.Text.Split('/');
    DateTime date = new DateTime(int.Parse(tabdate[2]), int.Parse(tabdate[1]), int.Parse(tabdate[0]));

    //In my dataset "Demande", on my table "Demande", i add a new row type "demande" with all good parameters
    ds_Demande.Demande.AddDemandeRow(TB_demande_ID.Text, TB_user_ID.Text, TB_nom_fichier.Text, int.Parse(TB_poids_fichier.Text), date);


    ta_Demande.Update(ds_Demande);//update the database
    LabelResponse.Text = "Ajout effectué avec succès";
    GridViewDemande.DataBind();//refresh gridview
}

protected void ClearTable_Click(object sender, EventArgs e)
{
    ds_Demande.Demande.Clear();//doesn't seem to do anything
    ta_Demande.Update(ds_Demande);
    GridViewDemande.DataBind();
}

回答1:

// Clear all rows of each table.
ds_Demande.Clear();

// alternatively:
foreach(var row in ds_Demande.Rows) {
   row.Delete();
}  


回答2:

for(int i = 0; i<ds_Demande.Count; i++)
{
     ds_Demande.Rows[i].Delete();
}