Click here to Skip to main content
14,739,737 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I have an huge data in a table now i want to add/update to the table.

I created the list of class for newly added data.

now want to insert the class data may be all some value need to be update rest of the inserted.

Which is the best approach?

What I have tried:

I try to basic approach using for loop but i think it not optimize way.
Posted
Updated 8-Jun-16 22:38pm
Comments
Tomas Takac 9-Jun-16 2:55am
   
Not clear. Sounds like you want to insert/update a single record in the table. Correct? Does the table have a primary key?

1 solution

Since you didn't specify WHICH database its hard to give you hard and fast solution. In general, you have three ways:

1. Send all of the data for insert and update into the database - you need stored procedure with table (in SQL Server) or XML parameter - this way, all your processing is done in the database - SQL Server can treat XML as a table so you have bulk insert / update. This is the way I did it in 2010.

2. Use .NET specific classes SqlBulkInsert (or equivalents for your database) to send the data (you have to separate what is insert and what is update before sending) - NEVER USED, so I cannot provide any details

3. Send the data row by row, you can either decide in code (via RowState.Added, RowState.Modified properties) or in the database. If former, you need two stored procedures (or queries), one for insert, second for update). Or you can send the data into stored procedure and decide there. DB2 has a nice command MERGE which can do both at the same time :)

I hope this helps somewhat. Good luck
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900