I am trying to delete the data present in the dataset with following code:
stateflowDataSet dsobject = new stateflowDataSet();
stateflowDataSetTableAdapters.dbo_statetableTableAdapter statetableadapter = new stateflowDataSetTableAdapters.dbo_statetableTableAdapter();
statetableadapter.Fill(dsobject.dbo_statetable);
dsobject.dbo_statetable.Clear();
statetableadapter.Update(dsobject);
But after this line when use statetableadapter.Fill(dsobject.dbo_statetable); the data is still retained.
Is the way in which I am clearing the data right?
Is there any other problem with the code?
Removing
DataRowsfrom aDataTabledoes not mean that you’ll delete them from your DBMS. The opposite is true, actually you’re preventing them from being deleted even if you would have calledDataRow.Delete()before.The reason is: only
DataRowsthat belong to aDataTablecan be deleted byThis will delete every row in the table with
DataRowState=Deleted.Therefor you need to use the
Deletemethod.You could do it also in one batch which would be more efficient, therefor you need to set the DataAdapter’s
UpdateBatchSizeto 0(unlimited).Another way would to delete all rows is to use a simple
SqlCommandwith CommandTextDELETE FROM Table:Now you need to remove(not delete, what is the core of your question) the rows from the
DataTablemanually since you’ve also deleted them manually in the database: