MySqlBackup.NET - MySQL Backup Solution for C#, VB.NET, ASP.NET
1. Introduction
This article introduce a tool (DLL) that can backup/restore MySQL database in C# or VB.NET and some sample codes on how to use it. It is an alternative to MySqlDump.Another benefits of making this tool is, we don't have to rely on two small programs - MySqlDump.exe and MySql.exe to perform the backup and restore task. We will have better control on the output result.
The most common way to backup a MySQL Database is by using MySqlDump.exe and MySQL Workbench.
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 used for Web applications. As most web hosting providers forbid that, MySqlBackup.NET will be helpful in building a web-based (ASP.NET/Web-Services) backup tool.
2. Features & Dependencies
Features
- Backup and Restore of MySQL Database
- Can be used in any .NET Languages.
- Export/Import to/from MemoryStream
- Conditional Rows Export (Filter Tables or Rows)
- Build-In Internal Encryption Function
- Able Restore to New Non-Existed Database
- Progress Report is Available for Both Export and Import Task.
- Able to export rows into different mode. (Insert, Insert Ignore, Replace, On Duplicate Key Update, Update)
- Can be used directly in ASP.NET or web services.
Prerequisite / Dependencies
- 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).
3. Background
This article is assumed that you have already familiar with MySQL dot net connector (MySql.Data.dll) with minimum knowledge that you are able to perform the four basic operationSELECT
, INSERT
, UPDATE
and <code>DELETE
. In case you are not, you can read the walk-through and explanation on Connecting C# to MySQL4. Basic Usage
Add this using statement before coding with MySqlBackup.NET:
Hide Copy Code
using MySql.Data.MySqlClient;
Simple Export Example
Hide Copy Code
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();
}
}
}
Simple Import Example
Hide Copy Code
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();
}
}
}
Above examples will export and import a MySQL database with default
options. There are some options that can modify the export and import
behavior. These options are defined in:MySqlBackup.ExportInfo
MySqlBackup.ImportInfo
- Create new database
- Only export table's structures
- Don't export rows of data
Download Link
0 Commentaires