How to perfrom batch deletes with ADO.Net

Hi,

I'm using code in the same way the following code sample to perform batch insert:

DataTable t = new DataTable();
t.Columns.Add("a", typeof(int));
t.Rows.Add(1);
t.Rows.Add(2);
// don't hardcode connection strings in your apps, this is just a quick sample :)
// pre-create the table with "CREATE TABLE t (a INT)"
using(OracleConnection conn = new OracleConnection("server=tcp:localhost; integrated security=true; database=test")) {
conn.Open();
OracleDataAdapter da = new OracleDataAdapter (null, conn);
da.InsertCommand = new OracleCommand("INSERT INTO t VALUES (@a)", conn);
da.InsertCommand.Parameters.Add("@a", SqlDbType.Int, 0, "a");
da.InsertCommand.UpdatedRowSource = UpdateRowSource.None;
da.UpdateBatchSize = 10;
da.Update(t);
}

How does I use batch ability of ADO.Net to perform batch deletes the same way I've performed batch inserts?

Thanks,

Michael.
[1817 byte] By [MichaelBachar] at [2007-12-23]
# 1


See the following documentation:

Performing Batch Updates with a DataAdapter

PaulPClementIV at 2007-8-30 > top of Msdn Tech,.NET Development,.NET Framework Data Access and Storage...
# 2

I have tried to to do what is described in this article by trying the DataTable with rows containing the values of the parameters matching the delete statement. It didn't work. It throws the following error: "Update requires valid InsertCommand when passed DataRow collection with new rows."

Thanks,

Michael.

MichaelBachar at 2007-8-30 > top of Msdn Tech,.NET Development,.NET Framework Data Access and Storage...
# 3

So did you add any rows to the DataTable and have you specified an InsertCommand?
PaulPClementIV at 2007-8-30 > top of Msdn Tech,.NET Development,.NET Framework Data Access and Storage...
# 4
I've added rows to the DataTable, I have specified only a delete command. My intention was to delete the appropriate entries in the table according to the values in the DataTable rows (same as I did with insert command).

Michael.
MichaelBachar at 2007-8-30 > top of Msdn Tech,.NET Development,.NET Framework Data Access and Storage...
# 5

Well if you're adding rows you're going to need a valid InsertCommand SQL statement.
PaulPClementIV at 2007-8-30 > top of Msdn Tech,.NET Development,.NET Framework Data Access and Storage...
# 6
I got it... this is why I started this thread. I don't need to perform insert in this case. When doing this with InsertCommand (as described my first message) I actually defined an array of values (the rows in the DataTable) to match the parameter in the insert SQL statement: "INSERT INTO t VALUES (@a)". The final result was batch insert according to the rows I add to the DataTable (even though I've defined only a single insert SQL statement). I want to do the same thing with DeleteCommand: "DELETE FROM t WHERE a=@p"... is this possible? Can I define an array of values to match the parameter in the delete SQL statement? The MSDN article http://msdn2.microsoft.com/en-us/library/kbbwt18a.aspx doesn't specify how to add values to the DataTable which according to it batch delete will be performed. Maybe I'm missing something.... Is there a code sample for this somewhere?

Thank you very much,

Michael.
MichaelBachar at 2007-8-30 > top of Msdn Tech,.NET Development,.NET Framework Data Access and Storage...
# 7


I might have misunderstood your question. Batch deletes (from the database) occur as a result of rows being marked for deletion from the DataTable. The rows that are marked for deletion are rows in the DataTable that initially resulted from a SELECT query.

If you're asking whether you can add new rows to the DataTable that you want deleted from the database, AFAIK it doesn't function that way.

PaulPClementIV at 2007-8-30 > top of Msdn Tech,.NET Development,.NET Framework Data Access and Storage...

.NET Development

Site Classified