Accent problem when doing mysql Dump in C# (latin1 and utf8)

Asked

Viewed 194 times

0

I made a program in c# that dump in mysql, it is very cool, it selects the bank and does the dump alone,making a sql file on my computer, and I just need to climb the bank later to where I wish. HOWEVER I’m having a problem when it comes time to take the bank to the place where I wish, (pelo cmd: mysql -u root -p -hNome_do_local < banco.sql), it is giving accentuation errors. Opening the dump made by my system I found out that it is putting:

/*!40101 SET NAMES latin1 /; when it was meant to be: /!40101 SET NAMES utf8 */;

I tried to solve by putting this in my code:

Server=xxxxxxx;Port=xxxxx;Database=xxx;Uid=xxxxx;Pwd=xxxx;charset=utf8;"

and

mb.ImportInfo.DatabaseDefaultCharSet="utf8";

but he keeps making a file with latin1

The program:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using MySql.Data.MySqlClient;
using System.IO.Ports;
using System.IO;
using System.IO.Compression;


namespace Sistema
{


public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();

        String caminhot = AppDomain.CurrentDomain.BaseDirectory;
        try
        {

            StreamReader lerserver = new StreamReader (caminhot + @"\server.log");
            sever.Text = lerserver.ReadToEnd();
            lerserver.Close();

            StreamReader leruser = new StreamReader(caminhot + @"\user.log");
            user.Text = leruser.ReadToEnd();
            leruser.Close();

            StreamReader sr = new StreamReader(caminhot + @"\port.log");
            porta.Text = sr.ReadToEnd();
            sr.Close();


        }
        catch
        {


        }
        try
        {
            StreamReader lercaminho = new StreamReader(caminhot + @"\caminho.log");
            caminho.Text = lercaminho.ReadToEnd();
            lercaminho.Close();
        }
        catch
        {

        }


    }

    private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
    {

    }

    private void button2_Click(object sender, EventArgs e)
    {

        String caminhot = AppDomain.CurrentDomain.BaseDirectory;

        StreamWriter escrevercaminho = new StreamWriter(caminhot + @"\caminho.log", false);
        escrevercaminho.WriteLine(caminho.Text);
        escrevercaminho.Close();

        DialogResult dr = MessageBox.Show(" Quer fazer o backup desse banco?\n\n" + comboBox1.Text + "\n\n Isso pode levar um tempo",
                 "Banco selecionado!", MessageBoxButtons.YesNo);
        switch (dr)
        {
            case DialogResult.Yes:

                string nomesever = (string)sever.Text;
                string nomeuser = (string)user.Text;
                string senha = (string)pass.Text;

                if (!Directory.Exists(caminho.Text + @"\Dumps\Arquivo_SQL"))

                    Directory.CreateDirectory(caminho.Text + @"\Dumps\Arquivo_SQL");

  if (!Directory.Exists(caminho.Text + @"\Dumps\Arquivo_ZIP"))

      Directory.CreateDirectory(caminho.Text + @"\Dumps\Arquivo_ZIP");



  var currentDateTime = DateTime.Now;

  string data = DateTime.Now.ToString("dd-MM-yyyy");
  {


      string file = caminho.Text + @"\Dumps\Arquivo_SQL\" + comboBox1.Text + data + ".sql";//cria sql

      using (MySqlConnection cn = new MySqlConnection())
      {

          cn.ConnectionString = ("server=" + nomesever + "; Port="+porta.Text+" ; pwd=" + senha + ";database=" + comboBox1.Text + "; uid=" + nomeuser + "; SslMode=none;charset=utf8;convertzerodatetime=true;");
          byte[] utf8Bytes = Encoding.UTF8.GetBytes("ASCII to UTF8");
          byte[] isoBytes = Encoding.Convert(Encoding.ASCII, Encoding.UTF8, utf8Bytes);
          string uf8converted = Encoding.UTF8.GetString(isoBytes);
          using (MySqlCommand cmd = new MySqlCommand())
          {
              using (MySqlBackup mb = new MySqlBackup(cmd))
              {
                  string fazendo = "fazendo backup";

                  MessageBox.Show(fazendo);
                  processo.Visible = true;
                  processo.Value = 10;
                  cmd.Connection = cn;
                  processo.Value = 20;
                  cn.Open();
                  processo.Value = 50;
                  processolab.Visible = true;
                  mb.ImportInfo.DatabaseDefaultCharSet ="utf8";
                  processo.Value = 65;
                  mb.ExportToFile(file);
                  processo.Value = 80;
                  cn.Close();
                  processo.Value = 90;

                  string startPath = caminho.Text + @"\Dumps\Arquivo_SQL";
                  string zipPath = caminho.Text + @"\Dumps\Arquivo_ZIP\" + comboBox1.Text + data + ".zip";

                  ZipFile.CreateFromDirectory(startPath, zipPath);
                  processo.Value = 100;
                  processolab.Visible = false;

                            }
                        }
                    }


                }

                MessageBox.Show("backup feito");
                //if (File.Exists(caminho.Text + @"\Dumps\Arquivo_SQL\" + comboBox1.Text + data + ".sql"))//exclue sql
                //    File.Delete(caminho.Text + @"\Dumps\Arquivo_SQL\" + comboBox1.Text + data + ".sql");
                break;
            case DialogResult.No:
                break;
        }


    }

    private void Form1_Load(object sender, EventArgs e)
    {

    }

    private void textBox1_TextChanged(object sender, EventArgs e)
    {

    }

    private void button1_Click(object sender, EventArgs e)
    {
        string nomesever = (string)sever.Text;
        string nomeuser = (string)user.Text;
        string senha = (string)pass.Text;




        try
        {

            MySqlConnection cn = new MySqlConnection();
            cn.ConnectionString = ("server=" + nomesever + "; Port=" + porta.Text + " ; pwd=" + senha + "; uid=" + nomeuser + "; SslMode=none;charset=utf8;convertzerodatetime=true;");
            cn.Open();
            MySqlCommand com = new MySqlCommand();
            com.Connection = cn;
            com.CommandText = "Show databases";
            MySqlDataReader dr = com.ExecuteReader();
            DataTable dt = new DataTable();
            dt.Load(dr);
            comboBox1.DisplayMember = "Database";
            comboBox1.DataSource = dt;
            cn.Close();



            MessageBox.Show("conectado, Selecione o banco");

            String caminhot = AppDomain.CurrentDomain.BaseDirectory;

            StreamWriter escreverserver = new StreamWriter(caminhot + @"\server.log", false);
            escreverserver.WriteLine(sever.Text);
            escreverserver.Close();

            StreamWriter escreveruser = new StreamWriter(caminhot + @"\user.log", false);
            escreveruser.WriteLine(user.Text);
            escreveruser.Close();

            StreamWriter escreverporta = new StreamWriter(caminhot + @"\port.log", false);
            escreverporta.WriteLine(porta.Text);
            escreverporta.Close();
        }
        catch
        {
            MessageBox.Show("Não conectado! \n verifique se os dados estão corretos.");
        }
    }

    private void clear_Click(object sender, EventArgs e)
    {
        sever.Text = "";
        user.Text = "";
        pass.Text = "";
        porta.Text = "";
        comboBox1.Text = "";
        caminho.Text = "";
        BT_Backup.Visible = false;


    }

    private void Busca_Click(object sender, EventArgs e)
    {
        FolderBrowserDialog busca = new FolderBrowserDialog();
        busca.RootFolder = Environment.SpecialFolder.Desktop;
        busca.Description = "Selecione a pasta desejada para armazear o backup do banco de dados";
        busca.ShowNewFolderButton = false;

        if (busca.ShowDialog() == DialogResult.OK)
        {
            caminho.Text = busca.SelectedPath;

        }
        BT_Backup.Visible = true;

    }   
}

}

I know there’s a lot on the show, but overall the problem is:

 string data = DateTime.Now.ToString("dd-MM-yyyy");
  {


      string file = caminho.Text + @"\Dumps\Arquivo_SQL\" + comboBox1.Text + data + ".sql";//cria sql

      using (MySqlConnection cn = new MySqlConnection())
      {

          cn.ConnectionString = ("server=" + nomesever + "; Port="+porta.Text+" ; pwd=" + senha + ";database=" + comboBox1.Text + "; uid=" + nomeuser + "; SslMode=none;charset=utf8;convertzerodatetime=true;");
          byte[] utf8Bytes = Encoding.UTF8.GetBytes("ASCII to UTF8");
          byte[] isoBytes = Encoding.Convert(Encoding.ASCII, Encoding.UTF8, utf8Bytes);
          string uf8converted = Encoding.UTF8.GetString(isoBytes);
          using (MySqlCommand cmd = new MySqlCommand())
          {
              using (MySqlBackup mb = new MySqlBackup(cmd))
              {
                  string fazendo = "fazendo backup";

                  MessageBox.Show(fazendo);
                  processo.Visible = true;
                  processo.Value = 10;
                  cmd.Connection = cn;
                  processo.Value = 20;
                  cn.Open();
                  processo.Value = 50;
                  processolab.Visible = true;
                  mb.ImportInfo.DatabaseDefaultCharSet ="utf8";
                  processo.Value = 65;
                  mb.ExportToFile(file);
                  processo.Value = 80;
                  cn.Close();
                  processo.Value = 90;

                  string startPath = caminho.Text + @"\Dumps\Arquivo_SQL";
                  string zipPath = caminho.Text + @"\Dumps\Arquivo_ZIP\" + comboBox1.Text + data + ".zip";

                  ZipFile.CreateFromDirectory(startPath, zipPath);
                  processo.Value = 100;
                  processolab.Visible = false;

                            }
                        }
                    }


                }

                MessageBox.Show("backup feito");
                //if (File.Exists(caminho.Text + @"\Dumps\Arquivo_SQL\" + comboBox1.Text + data + ".sql"))//exclue sql
                //    File.Delete(caminho.Text + @"\Dumps\Arquivo_SQL\" + comboBox1.Text + data + ".sql");
                break;
            case DialogResult.No:
                break;
        }


    } 

or in the:

 private void button1_Click(object sender, EventArgs e)
    {
        string nomesever = (string)sever.Text;
        string nomeuser = (string)user.Text;
        string senha = (string)pass.Text;




        try
        {

            MySqlConnection cn = new MySqlConnection();
            cn.ConnectionString = ("server=" + nomesever + "; Port=" + porta.Text + " ; pwd=" + senha + "; uid=" + nomeuser + "; SslMode=none;charset=utf8;convertzerodatetime=true;");
            cn.Open();
            MySqlCommand com = new MySqlCommand();
            com.Connection = cn;
            com.CommandText = "Show databases";
            MySqlDataReader dr = com.ExecuteReader();
            DataTable dt = new DataTable();
            dt.Load(dr);
            comboBox1.DisplayMember = "Database";
            comboBox1.DataSource = dt;
            cn.Close();



            MessageBox.Show("conectado, Selecione o banco");

            String caminhot = AppDomain.CurrentDomain.BaseDirectory;

            StreamWriter escreverserver = new StreamWriter(caminhot + @"\server.log", false);
            escreverserver.WriteLine(sever.Text);
            escreverserver.Close();

            StreamWriter escreveruser = new StreamWriter(caminhot + @"\user.log", false);
            escreveruser.WriteLine(user.Text);
            escreveruser.Close();

            StreamWriter escreverporta = new StreamWriter(caminhot + @"\port.log", false);
            escreverporta.WriteLine(porta.Text);
            escreverporta.Close();
        }
        catch
        {
            MessageBox.Show("Não conectado! \n verifique se os dados estão corretos.");
        }
    }
  • if you have any questions about my code or the question itself, please ask me, but I need help with this problem.

  • Put in the full code so we can understand how it was implemented.

No answers

Browser other questions tagged

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