Click here to Skip to main content
15,885,309 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

I have two sql servers(one of them is in my web server: server A, another one is local: server B)

I use the code bellow to copy data on a table in SQL SERVER A to another table in SQL SERVER B :
(different servers & different databases.. **the two tables are same in columns**)

ON for server A

OFF for server B
C#
string strsql = "SELECT * FROM **serverA_table**";
ONda = new SqlDataAdapter(strsql, ONcon);
ONds = new DataSet();
ONda.Fill(ONds, "serverA_table");


strsql = "SELECT * FROM serverB_table";
OFFda = new SqlDataAdapter(strsql, OFFcon);
OFFds = new DataSet();
OFFda.Fill(OFFds, "serverB_table");

DataRow[] newRow = ONds.Tables["serverA_table"].Select();

DataTable dtTarget = new DataTable();
dtTarget = OFFds.Tables["serverB_table"].Clone();

foreach (DataRow temp in newRow)
{
    dtTarget.ImportRow(temp);
}



SqlCommandBuilder cb = new SqlCommandBuilder(OFFda);
OFFda.InsertCommand = cb.GetInsertCommand();
OFFda.UpdateCommand = cb.GetUpdateCommand();
OFFda.DeleteCommand = cb.GetDeleteCommand();
OFFda.Update(OFFds.Tables["serverB_table"]);

But NOTHING happens and no row is added(imported) to my serverB_table !

please help me with this code or let me know a new way to do this action !

thanks in advance.
Posted
Updated 6-Mar-13 21:57pm
v2
Comments
Mohamad77 7-Mar-13 4:14am    
I used this code too, and no difference:

foreach (DataRow temp in ONds.Tables["serverA_table"].Rows)
{
OFFds.Tables["serverB_tanle"].ImportRow(temp);
}
OFFds.Tables["serverB_table"].AcceptChanges();

1 solution

If table schema is same you can use Marge method in your code. than add table to your data-set.

C#
 DataTable ONdt = new DataTable();
 DataTable OFFdt = new DataTable();
string strsql = "SELECT * FROM **serverA_table**";
           ONda = new SqlDataAdapter(strsql, ONcon);
           ONds = new DataSet();
           ONda.Fill(ONdt);


           strsql = "SELECT * FROM serverB_table";
           OFFda = new SqlDataAdapter(strsql, OFFcon);
           OFFds = new DataSet();
           OFFda.Fill(OFFdt);
OFFdt.Merge(ONdt);
// Than add it to your Dataset 
OFFds.Tables.Add(OFFdt);

C#

 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900