我正在尝试使用mysqldump从c#导出数据库。
当我运行它时,我收到此消息:选择数据库时未知数据库'mysqldump'。 我找不到解决方案。
public static void mysqlBackup()
{
try
{
//string time = DateTime.Now.ToString("dd-MM-yyyy");
Log.Info("Starting MySQL dump");
Process MySqlDump = new Process();
MySqlDump.StartInfo.FileName = @"mysqldump.exe";
MySqlDump.StartInfo.UseShellExecute = false;
MySqlDump.StartInfo.Arguments =
"mysqldump -uroot -p******** b3 >"+
" C:/Users/Administrator/Documents/temp/backups/backup.sql";
MySqlDump.StartInfo.RedirectStandardInput = false;
MySqlDump.StartInfo.RedirectStandardOutput = false;
MySqlDump.Start();
MySqlDump.WaitForExit();
MySqlDump.Close();
Log.Info("Successfull created");
}
catch (IOException ex)
{
Log.Error("Unable to write the database file" + ex.ToString());
}
}
我试图从相同的问题中删除mysqldump。
答案 0 :(得分:4)
重定向运算符>
不是mysqldump
的参数。当您在命令行上执行它时,它将由命令行本身解释,而不是由mysqldump解释。你有两个选择:
--result-file
选项RedirectStandardOutput
的{{1}}属性设置为StartInfo
来捕获流程的标准输出并使用输出执行您喜欢的操作。在此之后,您可以阅读流程的true
流。答案 1 :(得分:1)
我认为您需要指定要转储的数据库的名称作为第一个参数。感谢nathan,它最后会追上--databases
。
答案 2 :(得分:0)
Mysql文档说明有三种方法可以使用mysqldump命令:
shell> mysqldump [options] db_name [tbl_name ...]
shell> mysqldump [options] --databases db_name ...
shell> mysqldump [options] --all-databases
通过命令行确保命令正常工作。如果是这样,则直接在代码中执行该命令。如果可行,则开始提取您的参数并在代码中用您自己的参数替换它们。
基本上你想要尽可能基本,并从那里开始工作。
如果文件在命令行上有效,请尝试:
using (Process p = new Process())
{
p.StartInfo.FileName = @"mysqldump.exe -u root -p *** --database b3 -r test.sql"; <~~~ note the change here
p.Start();
p.WaitForExit();
}
除非您更改该代码,否则文件将被转储到项目文件夹bin / debug或bin / release文件夹中。
以下是您编辑过的方法:
public static void mysqlBackup() { 尝试 {
//string time = DateTime.Now.ToString("dd-MM-yyyy");
Log.Info("Starting MySQL dump");
using(Process MySqlDump = new Process()
{
MySqlDump.StartInfo.FileName = @"mysqldump.exe";
MySqlDump.StartInfo.UseShellExecute = false;
MySqlDump.StartInfo.Arguments = "-uroot -p******** b3 --result-file=C:/Users/Administrator/Documents/temp/backups/backup.sql";
MySqlDump.StartInfo.RedirectStandardInput = false;
MySqlDump.StartInfo.RedirectStandardOutput = false; //You can redirect this as mention in other answers
MySqlDump.Start();
MySqlDump.WaitForExit();
MySqlDump.Close();
}
Log.Info("Successfully created");
}
catch (IOException ex)
{
Log.Error("Unable to write the database file" + ex.ToString());
}
}
答案 3 :(得分:0)
MySqlDump.StartInfo.Arguments = "-u root -p *** database_name --result-file [path]\backup.sql";
你也不需要在命令中再次指定mysqldump(不是它应该有很大的不同)。