c# - How to copy data from server table to client table -


i want copy data server table client table.

i copy data client table client table like

insert newtable (col1, col2, col3) select column1, column2, column3 oldtable 

as know how copy data server table client table using linked server using ssms, want copy data using c# codes.

similar question :

how copy table data remote server local server in sql server 2008

any idea ??

edit :

now trying

  1. open remote server connection con1
  2. select column1, column2, column3 oldtable
  3. open client connection con2
  4. insert newtable (col1, col2, col3) values (con1.column1,con1.column2,con1.column3)

you can use sqlbulkcopy class efficiently copy data 1 table on sql server instance table on sql server instance.

here have simplest code that:

using (var sourceconnection = new sqlconnection(sourceconnectionstring)) using (var sourcecommand = new sqlcommand("select * sourcetable", sourceconnection)) using (var targetconnection = new sqlconnection(targetconnectionstring)) using (var bcp = new sqlbulkcopy(targetconnection, sqlbulkcopyoptions.tablelock, null))  {     bcp.destinationtablename = "targettable";     sourceconnection.open();     targetconnection.open();     using (var sourcereader = sourcecommand.executereader())     {         bcp.writetoserver(sourcereader);     } } 

you might need set other sqlbulkcopyoptions such keepidentity. might need set column mappings if table structure different.


Comments

Popular posts from this blog

python - Healpy: From Data to Healpix map -

c - Bitwise operation with (signed) enum value -

xslt - Unnest parent nodes by child node -