Update several records in a datatable

  • Thread starter Thread starter APich
  • Start date Start date
A

APich

Hi friends!

Is it possible update or delete a several records in a DataTable/DatSet
object using a simple SQL Update/Delete statement?

I try to see in the documentation but that I understand is that only record
by record making a loop it is possible, not using a SQL Statement.

If I do in this way (record by record, perhaps using a SELECT on the
DataTable for lookup the particular records) is reasonable fast?

I appreciate very much your help.

Abraham
 
Abraham:

Remember that the DataTable is totally detached from your database so no
matter what you do to it, if you don't use a DataAdapter to submit the
changes (or iteratively go through it and fire sequential SQL Statements),
those changes will never be shown in your Database. If I understand what
you want to do, it's fire all of those changes via sql. To do this, you can
use MyDataSet.GetChanges, this will give you a DataTable with only the
changes. You can overload the GetChanges to something like
GetChanges(DataRowState.Added) and then you'll only get the added rows, you
can then construct a SQL Statement to submit all of the Additions at once.

HTH, if not, I may have misunderstood your ultimate goal so let me know and
I can hopefully address it.

Bill
 
Hello,

By default, when you create the DataAdapter it only
has Select Command implement. Therefore, if you want
Update/Insert/Delete any row in DataTable you have to
implement the Update/Insert/Delete command(s). After
implement the command and assigned it to the DataAdapter
you only need to call the method Update() from
DataAdapter and it will do the work for you (implicitly)

-ecp-
 
As I understand your request, you want to apply the Delete to the DataTable,
not the underlying database.

The closest thing you can do is to use DataTable.Select to retrieve an array
of rows satisfying your criteria. You can then iterate through the array
calling the DataRow's Delete method which marks the row as deleted. Or
changing the values in fields, to simulate an update.

An easier solution, if you are only populating the dataset for this purpose,
is to create a command object to execute the Update/Delete SQL Statement and
then execute that directly without using a DataTable.

Neil.
 
Yes, that's is the idea

I want to make Updates or deletes in bulk in my DataTable after that I will
push my table to the database
OK, for me is clear, is not possible directly. The possible options are:

1) Update or Delete in the database and refresh the DataTable
2) Making a loop in memory using select for browse a rows collections and
made the updates ordeletes

Are there something else?

Thank you very much
 
Back
Top