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 8111763
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T02:14:35+00:00 2026-06-06T02:14:35+00:00

Hi all! I’m trying to synchronize two databases, one local and one remote. The

  • 0

Hi all!
I’m trying to synchronize two databases, one local and one remote. The problem is that I have two connections open at once and do not know how. If the two databases I have them locally if I correctly updated because they use the same connection. The problem I have is that the local board to update the program does not see it because the connection is not open, how I can do? This is my code.
Two databases have the same table estructure but user/pass and different database name. Thank you!

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using MySql.Data;
using MySql.Data.MySqlClient;

namespace sincronizacion
{
public partial class Form1 : Form
{
    private string stringConexionLocal ="server=localhost;database=kiosco_m;UID=root;pwd=toor";
    private string stringConexionRemota = "server="ipserverremote";database=grupoorb_kioscom;UID=user_remote;pwd=pass_remote";

    //Instanciado de objetos conexión local
    MySqlConnection conexionLocal;
    MySqlDataAdapter dataAdapterLocal;
    DataSet dataSetLocal;
    MySqlCommandBuilder builderLocal;

    //Instanciado de objetos conexión remota
    MySqlConnection conexionRemota;
    MySqlDataAdapter dataAdapterRemota;
    DataSet dataSetRemoto;
    MySqlCommandBuilder builderRemoto;

    public Form1()
    {
        InitializeComponent();
    }

    private void CargarDatosLocal()
    {
        string Consulta = "SELECT * FROM empresas";
        try
        {
            conexionLocal = new MySqlConnection(this.stringConexionLocal);
            dataAdapterLocal = new MySqlDataAdapter(Consulta, conexionLocal);
            dataSetLocal = new DataSet();
            dataAdapterLocal.Fill(dataSetLocal, "empresas");
            builderLocal = new MySqlCommandBuilder(dataAdapterLocal);
            dataGridView1.DataSource = dataSetLocal;
            dataGridView1.DataMember = "empresas";
        }
        catch (MySqlException ex)
        {
            MessageBox.Show(ex.Message, "Error al intentar conectarse a la BBDD local.", MessageBoxButtons.OK, MessageBoxIcon.Error);
        }

    }

    private void CargarDatosRemoto()
    {
        string Consulta = "SELECT * FROM empresas";
        try
        {
            conexionRemota = new MySqlConnection(this.stringConexionRemota);
            dataAdapterRemota = new MySqlDataAdapter(Consulta, conexionRemota);
            dataSetRemoto = new DataSet();
            dataAdapterRemota.Fill(dataSetRemoto, "empresas");
            builderRemoto = new MySqlCommandBuilder(dataAdapterRemota);
            dataGridView2.DataSource = dataSetRemoto;
            dataGridView2.DataMember = "empresas";

        }
        catch (MySqlException ex)
        {
            MessageBox.Show(ex.Message, "Error al intentar conectarse a la BBDD Remota.", MessageBoxButtons.OK, MessageBoxIcon.Error);
        }

    }

    private void Sincronizar(string miConexionRemota)
    {
        MySqlConnection miConexion = new MySqlConnection(miConexionRemota);
        miConexion.Open();
        MySqlCommand comando = new MySqlCommand();
        MySqlTransaction transaccion;

        // Empieza la transacción
        transaccion = miConexion.BeginTransaction(System.Data.IsolationLevel.ReadCommitted);
        comando.Transaction = transaccion;
        comando.Connection = miConexion;

        try
        {
            comando.CommandText = "UPDATE kiosco_remoto.empresas INNER JOIN kiosco_m.empresas ON kiosco_remoto.empresas.Id = kiosco_m.empresas.Id SET kiosco_remoto.empresas.direccion = kiosco_m.empresas.direccion";
            comando.ExecuteNonQuery();
            transaccion.Commit();
            MessageBox.Show("Se han sincronizado las BBDD correctamente.", "Información", MessageBoxButtons.OK, MessageBoxIcon.Information);
        }
        catch (MySqlException ex)
        {
            transaccion.Rollback();
            MessageBox.Show(ex.Message, "Error al intentar sincronizar.", MessageBoxButtons.OK, MessageBoxIcon.Error);
            //throw ex;

        }
        finally
        {
            CargarDatosLocal();
            CargarDatosRemoto();
            miConexion.Close();
        }

    }

    /* * *
     * 
     * Eventos
     *
     * * */

    private void botonLocal_Click(object sender, EventArgs e)
    {
        CargarDatosLocal();
    }

    private void botonCargaDatosRemoto_Click(object sender, EventArgs e)
    {
        CargarDatosRemoto();
    }

    private void botonGrabarBDLocal_Click(object sender, EventArgs e)
    {
        try
        {
            builderLocal.GetUpdateCommand();
            dataAdapterLocal.Update(dataSetLocal, "empresas");
            CargarDatosLocal();
        }
        catch (NullReferenceException ex)
        {
            MessageBox.Show(ex.Message, "No hay ninguna BBDD abierta.", MessageBoxButtons.OK, MessageBoxIcon.Error);
        }
    }

    private void botonGrabarBDRemota_Click(object sender, EventArgs e)
    {
        try
        {
            builderRemoto.GetUpdateCommand();
            dataAdapterRemota.Update(dataSetRemoto, "empresas");
            CargarDatosRemoto();
        }
        catch (NullReferenceException ex)
        {
            MessageBox.Show(ex.Message, "No hay ninguna BBDD abierta.", MessageBoxButtons.OK, MessageBoxIcon.Error);
        }
    }

    private void Form1_FormClosing(object sender, FormClosingEventArgs e)
    {
        try
        {
            conexionLocal.Close();
            conexionRemota.Close();

            if ((conexionLocal != null) || (conexionRemota != null))
            {
                MessageBox.Show("Se han cerrado todas las conexiones abiertas.", "Información", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }
        catch
        {
            // No hago nada.
        }
    }

    private void botonSincro_Click(object sender, EventArgs e)
    {
        Sincronizar(stringConexionRemota);
    }      
}

}

  • 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-06T02:14:36+00:00Added an answer on June 6, 2026 at 2:14 am

    Sadly I don’t quite follow your code as I don’t know the function names, however yes you can have two or more connections to MySQL open.

    To synchronize the data is a tricky one, if you want to do a one-way synch then this wouldn’t be difficult to do, but if you wanted a two way then you would have to handle a lot of exceptions for when there is duplicate data.

    If you want to get data from local database to a remote database then something like this would work:

    string const query = "SELECT * FROM LocalTableName";
    using (DbCommand sql =_MySQLConnection.CreateCommand()){
        sql.CommandText = query;
        using (DbDataReader row = sql.ExecuteReader()){
            // the data is sstored in row now upload to remote
            using (DbCommand sql_remote = _MySQLRemote.CreateCommand()){
                sql_remote.CommandText = "INSERT INTO RemoteTableName SET field1 = @p_field1";
                sql.Parameters.Add("@p_field1", row["field1"].toString());
                sql.ExecuteNonQuery();
                sql.Parameters.Clear();
            }
        }
    }
    

    sorry it’s untested and may have coding errors as I’m not infront of VS to test

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

Sidebar

Related Questions

All, I currently have two projects that are under SourceSafe that I am unable
All -- I have these two methods in one of my classes: public static
All, I have some script tags that are not working in Wordpress. If I
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
All, I have a form with a MultiValueField that almost works. It uses a
all. We're trying to get some intersect collisions working, but the problem experience is
All source code hosting services have size limits. I am wondering that does that
All- I have two spinners side by side. I want it to be so
All my searches returned nothing and I find it odd that there aren't any
All I am trying to do is take a standard range on an excel

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.