开发者

Importing non duplicate records from a XML file to a SQL server with LINQ

I have a SQL server database with th开发者_运维百科e following structure:

[Order]
OrderId
CustomerId
(...)

[OrderDetail]
OrderDetailId
OrderId
ProductId
Quantity 
(....)

I want to import an xml file like this that can contain records already inserted in the database:

<import>
    <orders>
        <order Id="1" />
        <order Id="2" />
        <order Id="3" />
    </order>
    <orderDetails>
        <orderDetail id="1" orderId="1" product="123" quantity="1" />
        <orderDetail id="2" orderId="1" product="456" quantity="5" />
    </orderDetails>
</import>

What is the most optimal way to read the xml file with LINQ2XML and check the database and insert only the non duplicate (new) rows to the database using LINQ2SQL?


Since both of these tables have ID columns, you could just surround the insertion method with a try/catch and trap any PK exceptions. Sounds fairly efficient to me.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜