Click here to Skip to main content
15,890,717 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi,

I have 10000 Data rows as a Data Table in my C#.Net Application.

I have the similar table to SQl Data base

I need to update the table if new record then insert into sql , if it is existing data i need to update the values.

Please suggest the efficinet way to do this

Thanks
Mohan
Posted
Comments
Rob Philpott 17-Jul-14 8:15am    
I do this sort of thing a lot. What you want to avoid is making 10,000 calls. Just do one. Are you using SQL Server and is it 2008 or above? Are you using stored procs?
V Mohan 17-Jul-14 8:22am    
Thanks rob.

I am using .Net 3.5 and SQL Server 2008

Thanks
Mohan
V Mohan 17-Jul-14 8:22am    
Yes i am using stored procedure

OK then.

Since SQL Server 2008, there is a thing called table parameters whereby you can send up your entire datatable to a stored procedure in a single go. The stored proc can use a join or whatever to do the comparison and update/insert accordingly.

Working with batch data like this rather than making individual calls is lightning quick.

The basic steps are this:

create a user-defined table type in SQL Server which matches that of the table.
SQL
CREATE TYPE [dbo].[CustomerTable] AS TABLE(
    [CustomerId] [int] NOT NULL,
    [Field1] [int] NULL,
    [Field2] [int] NULL,

etc.

You can use this type like any other as a parameter to your stored procedure. In the stored proc, work out how to do the reconciliation and put the whole thing in a transaction. It should take just a single parameter
create proc UpdateCustomers
(
	@customers CustomerTable readonly
	...



Then to call it, use ADO as you would normally. Just pass your datatable as the single parameter and mark it as type SqlDBType.Structured.

Bit short on time now, but let me know if more explanation is required. Google will help.
 
Share this answer
 
Hi,

For Bulk insert / SQL activities you can refer below link

Handling BULK Data insert from CSV to SQL Server[^]

Hope this will be helpful to you.
 
Share this answer
 
 
Share this answer
 

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