I need to write a method in C# to save all rows in a given datatable back to the database, but I cannot depend on the rowstate of each row. I can’t think of how best to do this without extensive looping through the database table data or constant database querying.
I want to essentially perform the following insert / update for each row in the datatable, but more efficiently:
INSERT INTO table
( col1,
col2 )
SELECT
'value1',
'value2'
FROM dual
WHERE NOT EXISTS ( SELECT * FROM table WHERE col1 = 'value1' );
UPDATE table
SET col2 = 'value2'
WHERE col1 = 'value1';
I was thinking a structure like this would be the most efficient:
private void SaveDataTable(DataTable dataTable)
{
//Remove data from the dataTable where it already exists in the database
//Determine if the values are to be inserted or updated
//Save relevant data
}
Any ideas on how best to accomplish this?
While I am unable to avoid querying for each row in the given datatable, I think this will work for now. Thanks for your responses; they were all very helpful.
(Oracle specific):