Backup Database - error in SQL Syntax

Asked

Viewed 40 times

1

I am building a button to make a backup of mine dbvia application C#, everything is going well, but at the time of applying the Executenonquery is accusing error of syntax.

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;
        }
    }

Back up:

    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 the path(Path) is already in the box.

Error:

inserir a descrição da imagem aqui

Ps: Same direct on Mysql - SQL Editor this syntax is not working; strange, because every tutorial I see, is exactly the same.

  • You can post the generated command?

  • What do you mean @LINQ ? this is all my command, all I do before is open the connection and designate the path within the textbox.

  • The SQL generated, young.

  • 1

    The value of the variable cmd.

  • cmd = "backup database [" + data + "] to disk='" + textBox1.Text + " " + "database" + "-" + Datetime.Now.Tostring("yyyy-MM-dd-HH-mm-ss") + ". Bak'";

  • 1

    The value, young man, the value, at runtime. The generated SQL command.

  • "backup database [eaglemotors] to disk='C: Backup database-2017-08-23-13-40-37.Bak'"

  • This is to be Mysql or SQL Server?

  • Mysql @LINQ.....

  • This is a statement from SQL Server, not Mysql.

  • Putz ! @LINQ que mancada heim. , thanks for identifying the error so, I’ll get the Mysql.

Show 6 more comments

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!");
                }
            }
        }


    }

Browser other questions tagged

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