Is it a good idea to re-use a dataset in .net? What I mean is…I have the following code which loops a gridview
For Each row in GridView
ds = New DataSet
ds.ReadXML(GetStream(row.Field))
... export the dataset
Next
What I am doing in the loop is creating a new instance of the DataSet. Can’t I just call ds.Clear() and then reuse it via the ds.ReadXML()?
The semantics are different:
DataSet.Cleardeletes all the data (rows), but keeps the schema (tables and relations).In your sample, it looks like the tables are being created from the ReadXml method (which can read schema as well as data).
DataSet.Clearwould probably work as long as you are certain all Xml documents have the same schema, but usingNewis more robust and expresses your intent better.However if you were reading data only, as in the following sample,
DataSet.Clearmight be a better choice as it avoids you repeatedly generating the schema.