?
Solved

if record exists update else insert using LINQ

Posted on 2008-06-10
5
Medium Priority
?
7,865 Views
Last Modified: 2013-11-11
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
0
Comment
Question by:idsnews
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
5 Comments
 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 21754255
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
0
 

Author Comment

by:idsnews
ID: 21754385
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.
0
 
LVL 143

Assisted Solution

by:Guy Hengel [angelIII / a3]
Guy Hengel [angelIII / a3] earned 400 total points
ID: 21754438
with SQL Server, using XML means using OPENXML() function to get a table-valued output from XML input.
as from SQL 2005, you could also use the xml data type, and perform XQuery on that to again have "table" data.

once you have that, the MERGE can be brought into play ...
0
 
LVL 21

Accepted Solution

by:
naspinski earned 1600 total points
ID: 21754567
If you want to use linq:
//instead of this foreach<int> you will do a foreach<XElement> or something else to run through your xml entries
//Also, you will obviously have to change my item class to what you want
foreach (int xmlID in xmlEntries)
{
    try
    {
        item i = (from p in db.items where p.itemID == xmlID select p).First();
        //it is present in the database -- now edit it
    }
    catch
    { 
        //it wasn't there, so insert it
    }

Open in new window

0
 

Author Closing Comment

by:idsnews
ID: 31465893
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
0

Featured Post

Get free NFR key for Veeam Availability Suite 9.5

Veeam is happy to provide a free NFR license (1 year, 2 sockets) to all certified IT Pros. The license allows for the non-production use of Veeam Availability Suite v9.5 in your home lab, without any feature limitations. It works for both VMware and Hyper-V environments

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …
Suggested Courses

801 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question