2

I am looking for a way how I can via c# back up some database from mysql (file backup). And also via c# restore database from backup file to some new location.

Can you help me with some ideas how to get started here .

Thanks.

4
  • Basically you'd want to replicate mysqldump from within C#, at which point you'd be better off just invoking mysqldump. Commented Nov 20, 2010 at 17:43
  • I have found now this one I think it will do the job for me Commented Nov 20, 2010 at 18:03
  • codeproject.com/KB/database/ConnectCsharpToMysql.aspx Commented Nov 20, 2010 at 18:03
  • @MarcB Some ASP.NET web hosting prevent the use of MySqlDump to be executed on their server. Commented May 5, 2018 at 1:08

4 Answers 4

4
ProcessStartInfo startInfo = new ProcessStartInfo("cmd.exe");
Process.Start(startInfo);
startInfo.Arguments = "mysqldump -u admin -p admin test > c:\backupfile.sql";
Process.Start(startInfo);

You can hide the dos prompt with startInfo.WindowStyle if you need.

Sign up to request clarification or add additional context in comments.

Comments

3

As alternative to MySqlDump, you can try MySqlBackup.NET: https://github.com/MySqlBackupNET/MySqlBackup.Net

Example

Backup/Export a MySQL Database

string constring = "server=localhost;user=root;pwd=qwerty;database=test;";

// Important Additional Connection Options
constring += "charset=utf8;convertzerodatetime=true;";

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

Import/Restore a MySQL Database

string constring = "server=localhost;user=root;pwd=qwerty;database=test;";

// Important Additional Connection Options
constring += "charset=utf8;convertzerodatetime=true;";

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

Comments

2

The CodeProject you found does backups by calling mysqldump.exe and does restores by calling mysql.exe from within a C# program (as Marc B recommended).

As an alternative, this CodeProject actually generates the SQL statements itself instead of calling an external program:

(It's not as fast or reliable as using mysqldump.exe / mysql.exe, but you can learn a lot from it.)

1 Comment

Hi, there is a new version (1.3) of the software(link) that you've provided. Whats your comment of that version?
0

You could try http://www.devart.com/dotconnect/mysql/docs/Devart.Data.MySql~Devart.Data.MySql.MySqlDump.html

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.