Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8029167
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T00:21:18+00:00 2026-06-05T00:21:18+00:00

I have 2 tables one is in local database and other one is on

  • 0

I have 2 tables one is in local database and other one is on remote server.
I use port forwarding in C# to get my ssh remote connection to the localhost:3306 my local database is on localhost:3307

How can I copy one table to another one if they are both on localhost but with different ports.

This is my query that I want to do:

cmd = new MySqlCommand(String.Format("INSERT INTO {0} (a,b,c,d) SELECT (a,b,c,d) FROM {1}", ConfigSettings.ReadSetting("main_table"), ConfigSettings.ReadSetting("main_table")), con);

Both tables have same columns and that is why I am using only one input from config settings.

But I have 2 connections strings as they are on different ports.

con.ConnectionString = ConfigurationManager.ConnectionStrings["con1"].ConnectionString;
con2.ConnectionString = ConfigurationManager.ConnectionStrings["con2"].ConnectionString

Is there any way to get my query to work with those 2 ports as I don’t want to be using FEDERATED connection in MySql. There must be the way as they are both on the same server.

Thank you

EDIT:

Connection strings:

<add name="Con2" connectionString="server=localhost;port=3307;user id=root;Password=*****;database=data" providerName="MySql.Data.MySqlClient"/>
<add name="Con1" connectionString="server=localhost;port=3306;user id=root;Password=******;database=data" providerName="MySql.Data.MySqlClient"/>
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T00:21:19+00:00Added an answer on June 5, 2026 at 12:21 am

    Pseudocode to move data between two separate tables on two different connection

    1. Create/Open the connection to the first MySql
    2. Create/Run a query to get data inside a DataTable from first MySql
    3. Create/Open the connection to the second MySql
    4. Create/Run a query to get data inside a DataTable from second MySql without records (using WHERE 1=0 or something like that)
    5. Loop on the rows of the first DataTable
    6. Call ImportRow on the second table passing the values from the
      first row
    7. Save the second datatable

      DataTable dtSource = new DataTable();
      using(MySqlConnection cn1 = GetConnection("first_connection_string");
      {
          cn1.Open();
          MySqlCommand cmd = new MySqlCommand("Select x,x,x,x from t1", cn1);
          MySqlDataAdapter da = new MySqlDataAdapter();
          da.SelectCommand = cmd;
          da.Fill(dtSource);
      }
      
      DataTable dtDest = new DataTable();
      using(MySqlConnection cn2 = GetConnection("second_connection_string");
      {
          cn2.Open();
          MySqlCommand cmd = new MySqlCommand("Select x,x,x,x from t2 where 1=0", cn2);
          MySqlDataAdapter da2 = new MySqlDataAdapter();
          da2.SelectCommand = cmd;
          da2.Fill(dtDest);
      
          foreach(DataRow r in dtSource)
              dtDest.ImportRow(r);
      
          da2.Update(dtDest);
      }
      

    Please note that this has not been tested. Take it as experimental.

    EDIT: Seeing the comments I will add another way to update the second table

        using(MySqlConnection cn2 = GetConnection("second_connection_string");
        {
            cn2.Open();
            MySqlCommand cmd = new MySqlCommand("INSERT INTO T2 (a,b) values(@a,@b)", cn2);
            cmd.Parameters.AddWithValue("@a", string.Empty); // Suppose the "a" field is a string
            cmd.Parameters.AddWithValue("@b", string.Empty); // Suppose the "b" field is a string
    
            foreach(DataRow r in dtSource)
            {
                cmd.Parameters["@a"].Value = r["a"].ToString();
                cmd.Parameters["@b"].Value = r["b"].ToString();
                cmd.ExecuteNonQuery();
            }
        }
    

    This is in the case you don’t need to insert a whole record from the dtSource in the destination table. In this case I revert to an INSERT command with parameters, loop on the source table, insert in the destination table

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have two tables : one table it matchs and the other is teams.
I have SMO code which copies tables from one database to another. It runs
So the scenario is this: I have a mySQL database on a local server
I have a (local) database (MySQL 5.1 at Ubuntu 10.10) with some 15000 tables
I have a (local) database (MySQL 5.1 at Ubuntu 10.10) with some 15000 tables
I have two tables: one hostgroup_host and the other one hostgroups. The hostgroups represents
I have two database tables, one in MYSQL and one in MSSQL. Both have
I have a local (read and write) sqlite database and a remote (read-only) oracle
I have a local SQL Server Express database that I want to copy to
I have 2 tables - one storing user information (id, username, password) and the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.