Performing Insert OR Update (upsert) on sql server compact edition

后端 未结 7 1740
陌清茗
陌清茗 2020-12-17 06:36

I have c# project that is using sqlserver compact edition and entity framework for data access. I have the need to insert or update a large amount of rows, 5000+ or more to

7条回答
  •  孤街浪徒
    2020-12-17 07:17

    I'm not sure if this is feasible or not, as I haven't used the Entity Framework, but have you tried running the update first and checking the rowcount -- inserting if no rows were updated? This may be faster than catching exceptions. It's generally a bad practise to use exceptions for control flow, and often slows things down dramatically.

    If you can write the SQL directly, then the fastest way to do it would be to get all the data into a temporary table and then update what exists and insert the rests (as in Andrea Bertani's example above). You should get slightly better results by using a left join on the original table in the select in your insert, and excluding any rows with values from the original table that are not null:

    INSERT INTO original
    SELECT * FROM temp
    LEFT JOIN original ON original.id = temp.id
    WHERE original.id IS NULL
    

提交回复
热议问题