我从HERE发现了一个关于如何备份数据库的查询,但是它无法正常工作并且会引发错误。请帮忙。 TIA
CODE:
using MySql.Data.MySqlClient;
public bool BackupDatabase(string filename = "")
{
bool _isExecuted = false;
if (filename == "")
{
filename = string.Format("{0}/db_{1:MMddyyyyHHmmss}.sql", Environment.GetFolderPath(Environment.SpecialFolder.Desktop), DateTime.Now);
}
string _queryBackup = string.Format("mysqldump -u '{0}' -p '{1}' '{2}' > '{3}'", DatabaseUsername, DatabasePassword, DatabaseName, filename);
using (MySqlConnection con = new MySqlConnection(ConnectionString))
{
try
{
con.Open();
MySqlCommand cmd = new MySqlCommand(_queryBackup, con);
cmd.ExecuteNonQuery();
_isExecuted = true;
}
catch (Exception ex)
{
string _x = ex.Message;
}
}
return _isExecuted;
}
错误:
_x 包含此错误
“您的SQL语法有错误;请查看手册 对应于您的MySQL服务器版本,以便使用正确的语法 靠近'mysqldump -u'root'-p'usbw''my_database_name'> 第1行的'C:\ Users \ DesktopName \ Desktop \ db_08242012164022.sql'“
答案 0 :(得分:1)
您的_queryBackup不是sql查询字符串,而是从系统运行的字符串,就像您从cmd控制台运行它一样。
以下是一些从C#
运行命令的代码 var startInfo = new ProcessStartInfo(command, parameterstring);
startInfo.RedirectStandardError = true;
startInfo.CreateNoWindow = true;
startInfo.RedirectStandardOutput = true;
startInfo.UseShellExecute = false;
startInfo.ErrorDialog = false;
Process process = Process.Start(startInfo);
StreamReader stdoutSR = process.StandardOutput;
string stdout = stdoutSR.ReadToEnd();
StreamReader stderrSR = process.StandardError;
string stderr = stderrSR.ReadToEnd();