Copy from one database table to another C#

前端 未结 5 711
情话喂你
情话喂你 2021-01-02 10:03

Using C# (vs2005) I need to copy a table from one database to another. Both database engines are SQL Server 2005. For the remote database, the source, I only have execute ac

5条回答
  •  天涯浪人
    2021-01-02 11:02

    You probably can't do this, but if you can't, DON'T do it with a program. If you have any way of talking to someone who controls the source server, see if they will set up some sort of export of the data. If the data is as small as you say, then xml or csv output would be 100x better than writing something in c# (or any language).

    So let's assume they can't export, still, avoid writing a program. You say you have more control over the destination. Can you set up an SSIS package, or setup a linked server? If so, you'll have a much easier time migrating the data.

    If you set up at bare minimum the source as a linked server you could write a small t-sql batch to

    TRUNCATE DestTable

    INSERT INTO DestTable SELECT SourceTable.Star FROM [SourceServer].[Schema].[Table]

    wouldn't be as nice as SSIS (you have more visual of what's happening, but the t-sql above is pretty clear).

    Since I would not take the programming route, the best solution I could give you would be, if you absolutely had to:

    Use SqlClient namespace.

    So, create 2 SqlConnections, 2 SqlCommands, and get the instance of the 1 SqlReader.

    Iterate through the source reader, and execute the destination SqlCommand insert for each iteration with the.

    It'll be ugly, but it'll work.

提交回复
热议问题