Backup Database - error in SQL Syntax

1

I am building a button to back up my db via C# application, is running fine, however at the time of applying ExecuteNonQuery is accusing% error of%.

Follows:

Designate Path:

    private void Browser_Click(object sender, EventArgs e)
    {
        FolderBrowserDialog dlg = new FolderBrowserDialog();
        if (dlg.ShowDialog() == DialogResult.OK)
        {
            textBox1.Text = dlg.SelectedPath;
            FazerBackup.Enabled = true;
        }
    }

Backup:

    private void FazerBackup_Click(object sender, EventArgs e)
    {

       MySqlConnection conexao = ClassConexao.ObterConexao();

        try
        {


            string data = conexao.Database.ToString();

            string cmd = "backup database [" + data + "] to disk='" + textBox1.Text + "\" + "database" + "-" + DateTime.Now.ToString("yyyy-MM-dd--HH-mm-ss") + ".bak'";



            using (MySqlCommand command = new MySqlCommand(cmd, conexao))
            {
                if (conexao.State != ConnectionState.Open)
                {
                    conexao.Open();
                }
                command.ExecuteNonQuery();
                conexao.Close();
                MessageBox.Show("Sucesso");
            }
        }
        catch (Exception error)
        {
            MessageBox.Show(String.Format("Check the fields {0}", error.Message));
            return;
        }
    }

Connection is already open and Path (path) is already in the box.

Error:

Ps: Even straight into MySql - SQL Editor this syntax is not working; strange, because every tutorial I see, is exactly the same.

    
asked by anonymous 23.08.2017 / 18:51

1 answer

0

Answering my own question:

The error is actually in the command that is for SQLServer and not for MySQL.

follows the command for MYSQL:

    private void FazerBackup_Click(object sender, EventArgs e)
    {


        string conn = "server=HostDoSeuServer; database=SuaDataBase; Uid=SeiUser; pwd=SuaSenha";


        //String para gravar o Path Designado por um Browser:
        string file = textBox1.Text + "\database" + "-" + DateTime.Now.ToString("yyyy-MM-dd--HH-mm-ss") + "-.sql";

        using (MySqlConnection con = new MySqlConnection(conn))
        {
            using (MySqlCommand cmd = new MySqlCommand())
            {
                using (MySqlBackup mb = new MySqlBackup(cmd))
                {
                    cmd.Connection = con;
                    con.Open();
                    mb.ExportToFile(file);
                    con.Close();
                    MessageBox.Show("Sucesso!");
                }
            }
        }


    }
    
23.08.2017 / 21:07