1

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"/>
3
  • Removed my answer. I see that you want to perform the insert using only one connection. I don't know if it is possibile. Looking around if I find something.
    – Steve
    Jun 3, 2012 at 9:36
  • I look something that will make this work from c# so for example I can read select from one and save it somewhere and insert it into another table if necessary. Jun 3, 2012 at 9:38
  • If you are ok with using separate connections then you could read all of your required data inside a DataTable then create an empty datatable from the second connection, then copy data via LoadDataRow in the second table and then write back the new records of the second datatable.
    – Steve
    Jun 3, 2012 at 9:41

1 Answer 1

1

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

9
  • This is good example- In the second query I want to insert all from table one to table 2, Why do you select 2 times? Jun 3, 2012 at 10:20
  • In first query I have put SELEECT * from Data and in second one should go something like insert... Jun 3, 2012 at 10:25
  • The first query loads data, the second select serves only to give a schema to the datatable without data. Then the select command is used from the da2 to derive the INSERT and UPDATE commands to execute against data in dtDest
    – Steve
    Jun 3, 2012 at 11:07
  • I have put same queries int first and second one. Only on the second I didn't where statement, I am debugging and I can't get trough this part da.Fill(dtSource);- this is trowing me null exception. Do I need to initialize it somehow? Jun 3, 2012 at 11:14
  • Yes, put a dtDest = new DataTable(); instead of dtDest = null just before entering the second using statement
    – Steve
    Jun 3, 2012 at 11:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.