原文:http://www.codeproject.com/Articles/256466/MySqlBackup-NET
using MySql.Data.MySqlClient;
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();
}
}
}
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();
}
}
}
string constring = "server=localhost;user=root;pwd=1234;database=test1;";
string file = "Y:\\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.ExportInfo.AddCreateDatabase = true;
mb.ExportInfo.ExportTableStructure = true;
mb.ExportInfo.ExportRows = false;
mb.ExportToFile(file);
}
}
}
string constring = "server=localhost;user=root;pwd=1234;database=test1;";
string file = "Y:\\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.ImportInfo.TargetDatabase = "test2";
mb.ImportInfo.DatabaseDefaultCharSet = "utf8";
mb.ImportFromFile(file);
}
}
}
Sample code for Export. Below codes will export the content into MemoryStream, then transmit it directly for download.
using System.IO;
string connstr = "server=localhost;user=root;pwd=1234;database=test;";
MemoryStream ms = new MemoryStream();
using (MySqlConnection conn = new MySqlConnection(connstr))
{
MySqlCommand cmd = new MySqlCommand();
MySqlBackup mb = new MySqlBackup(cmd);
cmd.Connection = conn;
conn.Open();
mb.ExportToMemoryStream(ms);
}
Response.ContentType = "text/plain";
Response.AppendHeader("Content-Disposition", "attachment; filename=backup.sql");
Response.BinaryWrite(ms.ToArray());
Response.End();
string connstr = "server=localhost;user=root;pwd=1234;database=test;";
byte[] ba = FileUpload1.FileBytes;
MemoryStream ms = new MemoryStream(ba);
using (MySqlConnection conn = new MySqlConnection(connstr))
{
MySqlCommand cmd = new MySqlCommand();
MySqlBackup mb = new MySqlBackup(cmd);
cmd.Connection = conn;
conn.Open();
mb.ExportToMemoryStream(ms);
}
Header.Controls.Add(new LiteralControl("<script type=\"text/javascript\">alert('ok');</script>"));
More guides and examples available at the project site's documentation:
https://mysqlbackupnet.codeplex.com/documentation[^]
Below are some of the guides
- Example of Using in MemoryStream, Zip and ASP.NET
- Using Progress Report With Export/Backup
- Using Progress Report With Import/Restore
- Conditional Rows Export for Each Table
- Using Encryption
- FAQ - Commonly Seen Errors