Click here to Skip to main content
11,709,033 members (62,471 online)
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# SQL WinForm
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
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 6-Mar-13 21:50pm
Edited 6-Mar-13 21:57pm
v2
Comments
Mohamad77 at 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

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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

 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);
  Permalink  

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

  Print Answers RSS
0 Sergey Alexandrovich Kryukov 580
1 Maciej Los 235
2 Peter Leow 219
3 OriginalGriff 214
4 Mika Wendelius 170
0 OriginalGriff 9,348
1 Sergey Alexandrovich Kryukov 8,727
2 CPallini 5,189
3 Maciej Los 4,991
4 Mika Wendelius 3,856


Advertise | Privacy | Mobile
Web04 | 2.8.150819.1 | Last Updated 7 Mar 2013
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100