문제

I'm wanting to do a bulk copy of data from one database to another. It needs to be dynamic enough so that when the users of the source database create new fields, there are minimal changes at the destination end(my end!).

I've done this using the sqlbulkcopy function, using column mappings set up in a seperate table, so that if anything new is created all I need to do is create the new field and set up the mapping (no code or stored procedure changes):

foreach (var mapping in columnMapping)
{
    var split = mapping.Split(new[] { ',' });
    sbc.ColumnMappings.Add(split.First(), split.Last());
}

try
{
    sbc.WriteToServer(sourcedatatable);
}

However, the requirements have now changed.
I need to keep more data, sourced from elsewhere, in other columns in this table which means I can't truncate the whole table and write everything with the sqlbulkcopy. Now, I need to be able to Insert new records or Update the relevant fields for current records, but still be dynamic enough that I won't need code changes if the users create new fields.

Does anyone have any ideas?

도움이 되었습니까?

해결책

Comment on original question from mdisibio - it looks like the SQL MERGE statement would have been the answer.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top