Insert a select in c#

Asked

Viewed 28 times

1

Good night! Personal, I have a select working normally and displaying the result in a datagridviewer all straight though, I need to take the result of this query that is done in the mysql database of the X server and write in an identical table in the database of the Y server, how do I enter a select in c#?

Below is my query.

try
        {
            conexao = new MySqlConnection("server=xxxxx;database=xxxx;uid=xxx;pwd=xxxx;port=3306;SSL Mode = none");

            DateTime DataFechInicio = dtpFechInicio.Value;
            DateTime DataFechFim = dtpFechFim.Value;

            strSQL = "SELECT " +
                             "p.Nome," +
                             "v.Vencimento," +
                             "Concat('R$ ',Format(v.Valor,2,'de_DE')) as Valor," +
                             "bd.Descricao as 'Centro de Custo'" +
                     "FROM pessoa p " +
                                    "INNER JOIN pedido pd " +
                                                          "ON p.idpessoa = pd.fornecedor " +
                                    "INNER JOIN pedvenc v " +
                                                          "ON v.idpedido = pd.idpedido " +
                                    "INNER JOIN base_dpto bd " +
                                                             "ON bd.CCUSTO = v.CCUSTO " +
                     "WHERE v.vencimento " +
                               "BETWEEN '" + DataFechInicio.ToString("yyyy-MM-dd") + "' " +
                                       "AND '" + DataFechFim.ToString("yyyy-MM-dd") + "' " +
                                       "AND pd.cnsstatus = 'FECHADO' " +
                                       "AND pd.cnscanmom IS NULL " +
                      "ORDER  BY nome ASC";

            da = new MySqlDataAdapter(strSQL, conexao);

            DataTable dt = new DataTable();

            da.Fill(dt);

            dgvFechDados.DataSource = dt;

            conexao.Open(); 
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);

            throw;
        }
        finally
        {
            conexao.Close();
            conexao = null;
        }
  • 2

    Isn’t it easier to do it on the server anyway? It might be something like this insert into serverY.database.tabela(campos...) select campos from serverX.database.tabela

1 answer

0

Friend you will need to mount the Insert query and run it in the target database, just like you do select, but assemble the string and concatenate the value you acquired in select.

For example you did select which rerouted the following object array [{name:John, age:20}, {name:James, age:19}, {name:John, age 30}]

with this array you will build your query dynamically traversing the values and concatenating, then just run the query as well as select query

Browser other questions tagged

You are not signed in. Login or sign up in order to post.