A .NET Framework library used for backup, export, restore, import of MySql database. An alternative for MySqlDump.exe. Written in C#. Can be used in VB.NET and ASP.NET.
MySqlBackup.NET
is an open source project used as alternative to MySqlDump and MySql WorkBench for exporting and importing (backup & restore) of MySQL database in .NET environment.
Project Site: https://github.com/MySqlBackupNET/MySqlBackup.Net
Nuget:
PM> Install-Package MySqlBackup.NET
MySql Workbench is good for developers, but, when comes to client or end-user, the recommended way is to get every parameter preset and all they need to know is press the big button "Backup" and everything is done. Using MySQL Workbench as a backup tool is not a suitable solution for client or end-user.
On the other hand, MySqlDump.exe cannot be executed directly from the Web Server. As most providers forbid that, MySqlBackup will be helpful in building a web-based (ASP.NET) backup tool.
Prerequisite / Dependencies
MySqlBackup.NET is built on top of MySQL dot net Connector/Net (MySql.Data.DLL). A reference of this DLL must be added into your project in order for MySqlBackup.NET to work. MySql.Data.DLL is developed by Oracle Corporation, licensed under GPL License (http://www.gnu.org/licenses/old-licenses/gpl-2.0.html).
A Quick Example of Backup/Exporting 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();
}
}
}
A Quick Example of Restore/Importing 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();
}
}
}
Reminder 1
MySqlBackup.NET
stands on top of MySql.Data.DLL which also stands on top of .NET Framework, which uses UTF8 encoding by default. If your database involves any UTF8 or Unicode Characters. You must use a MySQL database with default character of UTF8 while handling Unicode Characters, such as
- Western European specific languages, the character of 'À', 'ë', 'õ', 'Ñ'.
- Russian, Hebrew, India, Arabic, Chinese, Korean, Japanese characters, etc.
You are recommended to apply the connection string option of charset=utf8
. Example:
server=localhost;user=root;pwd=mypwd;charset=utf8;
Reminder 2
DateTime
conversion between MySQL and .NET Framework. In MySQL, there are various of DateTime format, such as null
value or Date
only data. But, in .NET Framework, there is no null value (or Date only) for DateTime. A data conversion error (exception) will be thrown. This error is not caused by MySqlBackup.Net
. MySql.Data.DLL
is designed to throw this exception of data conversion error. Therefore, a connection string option of convertzerodatetime=true
is strongly recommended to be applied. Example:
server=localhost;user=root;pwd=mypwd;charset=utf8;convertzerodatetime=true;
For more reminders and support, please visit the Wiki page of the project site.