Link to home
Create AccountLog in
Avatar of idsnews
idsnews

asked on

if record exists update else insert using LINQ

I have an xml file that I parse and insert into the database. What I want to do is, if the records in the xml file already exist in the database I just want to update the database, If they don't exist I would like to insert them. I would like to do this as efficiently as possible either through LINQ and/or stored procedures, but can't find anything online. The xml file has IDs that correspond to database IDs.

Running ASP.NET 3.5 and MS Server 08

Thanks
Avatar of Guy Hengel [angelIII / a3]
Guy Hengel [angelIII / a3]
Flag of Luxembourg image

if you use SQL 2008, you should learn up the newly introduce MERGE statement:
http://www.builderau.com.au/program/sqlserver/soa/Using-SQL-Server-2008-s-MERGE-statement/0,339028455,339283059,00.htm
Avatar of idsnews
idsnews

ASKER

Thanks, but from the looks of it the MERGE statement is only useful for merging tables. Whereas I need to merge a table with an XML document.
SOLUTION
Avatar of Guy Hengel [angelIII / a3]
Guy Hengel [angelIII / a3]
Flag of Luxembourg image

Link to home
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
See answer
ASKER CERTIFIED SOLUTION
Link to home
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
Avatar of idsnews

ASKER

Thanks angelll for the MERGE solution but it seekemed a little over my head and the try catch with the LINQ was exactly what I was looking for because I was already parsing the XML file using linq and XElements