10

I created a Winforms in order to backup my Database. Then When I run my program it gives an Win32Exception was unhandled. "The system cannot find the file specified" Although the file is already existed and resulted to that exception.

Here is my code regarding my problem

using System.Diagnostics;

private void btnProceed_Click(object sender, EventArgs e)
{
            path = @"D:\MySQL\MySQL Server 5.5\bin\mysqldump.exe -u " + txtBoxDBUsername.Text + @" -p " + txtBoxDBName.Text + @" > D:\C#\Client\Salesmate - EMC\SalesMate\Backup\" + maskeTxtBoxDBFile.Text + @"";
            Process p = new Process();
            p.StartInfo.FileName = path;
            p.Start();
}
4

6 に答える 6

36

You can use MySqlBackup.NET as alternative to MySqlDump
Documentation:
http://www.codeproject.com/Articles/256466/MySqlBackup-NET-MySQL-Backup-Solution-for-Csharp-V
https://github.com/MySqlBackupNET/MySqlBackup.Net

Sample codes:

Backup a MySQL database

using MySql.Data.MySqlClient; 

then the code,

private void Backup()
{
    string constring = "server=localhost;user=root;pwd=qwerty;database=test;";
    string file = "C:\\backup.sql";
    using (MySqlConnection conn = new MySqlConnection(constring))
    {
        using (MySqlCommand cmd = new MySqlCommand())
        {
            using (MySqlBackup mb = new MySqlBackup(cmd))
            {
                cmd.Connection = conn;
                conn.Open();
                mb.ExportToFile(file);
                conn.Close();
            }
        }
    }
}


Restore a MySQL database

private void Restore()
{
    string constring = "server=localhost;user=root;pwd=qwerty;database=test;";
    string file = "C:\\backup.sql";
    using (MySqlConnection conn = new MySqlConnection(constring))
    {
        using (MySqlCommand cmd = new MySqlCommand())
        {
            using (MySqlBackup mb = new MySqlBackup(cmd))
            {
                cmd.Connection = conn;
                conn.Open();
                mb.ImportFromFile(file);
                conn.Close();
            }
        }
    }
}

Update:
I am one of the author of this library.

于 2012-09-07T04:21:25.763 に答える
1

I have try that code but problem is occure in before run. exeption is -An unhandled exception of type 'System.IO.FileLoadException' occurred in System.Windows.Forms.dll

Additional information: Could not load file or assembly 'MySql.Data, Version=6.5.4.0, Culture=neutral, PublicKeyToken=c5687fc88969c44d' or one of its dependencies. The located assembly's manifest definition does not match the assembly reference. (Exception from HRESULT: 0x80131040)

于 2015-02-17T05:29:09.817 に答える
0

I believe you have to mention the user, pwd, db name and the target path..

string path = @"C:\Program Files\MySQL\MySQL Server 5.5\bin\mysqldump.exe -u " + txtBoxDBUsername.Text + @" -p " + txtBoxDBName.Text + @" > " + txtBoxDBName.Text + @".sql"; 

backup: # mysqldump -u root -p[root_password] [database_name] > dumpfilename.sql

restore:# mysql -u root -p[root_password] [database_name] < dumpfilename.sql

http://www.thegeekstuff.com/2008/09/backup-and-restore-mysql-database-using-mysqldump/

于 2012-09-07T04:23:38.217 に答える
0

You may try this one.

    public void BackUpData(string file)
    {
        using (MySqlConnection con = new MySqlConnection { ConnectionString = config })
        {
            using (MySqlCommand cmd = new MySqlCommand { Connection = con })
            {
                using (MySqlBackup mb = new MySqlBackup { Command = cmd })
                {


                    try
                    {
                        con.Open();

                        try
                        {
                            mb.ExportToFile(file);
                        }
                        catch(MySqlException ex)
                        {
                            msgErr(ex.Message + " sql query error.");
                            return;
                        }
                    }
                    catch(MySqlException ex)
                    {
                        msgErr(ex.Message + " connection error.");
                        return;
                    }



                }

            }

        }

    }
于 2017-06-23T06:25:20.170 に答える
0
  • Don't putt the whole call inside "path = ", you should use "Arguments" to specify arguments, as name says. If library checks for presence of the called file (your whole path) it shouldn't find it!
  • Are you sure that path is correct? You should find MySQL Server path using registry, not hard-coding the path, or if it can be not easy for you you can pass it as an argument from command line or specify from your form (settings page).
  • You may have missed credentials: -u should be used for username (even if I use --user) and -p should be for password (even if I use --password). Why do you pass "txtBoxDBName.Text" as password?!
  • Maybe your destination path is invalid: it contains spaces, if you use spaces you should use quotes.
  • What does txtBoxDBName.Text (?password?) contains? Spaces too? If yes it doesn't work.
  • Last presence of + @"" is completely useless, it doesn't insert any quotes.

A correct version of your code with quotes corrected is: path = @"""D:\MySQL\MySQL Server 5.5\bin\mysqldump.exe"" -u " + txtBoxDBUsername.Text + @" -p " + txtBoxDBName.Text + @" > ""D:\C#\Client\Salesmate - EMC\SalesMate\Backup\" + maskeTxtBoxDBFile.Text + @"""";

For more readability: path = $@"""D:\MySQL\MySQL Server 5.5\bin\mysqldump.exe"" -u {txtBoxDBUsername.Text} -p {txtBoxDBName.Text} > ""D:\C#\Client\Salesmate - EMC\SalesMate\Backup{maskeTxtBoxDBFile.Text}""";

于 2018-06-07T08:28:48.127 に答える
0
ProcessStartInfo psi = new ProcessStartInfo();
psi.FileName = @"C:\xampp\mysql\bin\mysql.exe";
psi.RedirectStandardInput = true;
psi.RedirectStandardOutput = false;
psi.CreateNoWindow = true;
psi.Arguments = string.Format(@"-u{0} -p{1} -h{2} {3}", "root", "password", "localhost", "your_dbname");
psi.UseShellExecute = false;
Process process = Process.Start(psi);
process.StandardInput.Write(File.ReadAllText(inputFilePath));
process.StandardInput.Close();
process.WaitForExit();
process.Close();

This one worked for me you can try it out as long as you've your backed up .sql file

于 2018-06-08T08:02:42.123 に答える