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
- open remote server connection con1
select column1, column2, column3 oldtable
- open client connection con2
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
Post a Comment