I made a program in C # that does dump in mysql, it's very nice, it selects the database and dumps it alone, making an sql file on my computer, and I just need to move up the bank later where I want it.
BUT I'm having a problem uploading the bank to the location I want, (pelo cmd: mysql -u root -p -hNome_do_local < banco.sql
), it's giving accent errors. Opening the dump done by my system I found that he is putting:
/ *! 40101 SET NAMES latin1 /; being that 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 continues to make 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 is a lot in the program, but overall the problem is in:
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 not:
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.");
}
}