我使用c#作为编程语言和MySQL作为DBMS构建了一个Windows窗体应用程序,我想在用户单击时添加一个导出数据库的按钮。 到目前为止我创建的是:
MySqlCommand cmd = new MySqlCommand("SELECT * FROM client INTO OUTFILE '"+path+"' FIELDS TERMINATED BY ',' ENCLOSED BY '\"' LINES TERMINATED BY'\\n' ", con);
MySqlDataReader dataReader = cmd.ExecuteReader();
dataReader.Read();
但问题是这段代码只导出数据而不是表格的模式。是否有任何方法可以通过 SQL语句导出带有模式的数据库。
答案 0 :(得分:0)
您可以使用 MySqlBackup.NET
https://github.com/MySqlBackupNET/MySqlBackup.Net
样例代码:
备份MySQL数据库
using MySql.Data.MySqlClient;
然后输入代码
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();
}
}
}
}
还原MySQL数据库
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();
}
}
}
}