通过C#还原MySQL数据库 [英] Restore MySQL DB through C#
问题描述
我尝试使用进程从C#备份和还原MySQL数据库.
第一次备份:
备份数据库的命令是:
-u用户-pPASS-数据库DATABASE -r>路径/到/file.sql
这是我的C#代码:
I try to backup and restore MySQL database from C# using process.
First BackUP:
the command to backup database is :
-u USER -pPASS --databases DATABASE -r > path/to/file.sql
Here is my C# code:
Process.Start(@"C:\Program Files\MySQL\MySQL Server 5.5\bin\mysqldump.exe", ("-u user -p***** --databases DB -r \"" + path + "\"";
看一下区别,因为我的代码不是"> ",因为它不能使用它.如果我将此字符放入代码中,它将仅向我显示win cmd中mysqldump的用法.没有它,此代码将运行良好.
现在我想还原数据库.
恢复数据库的命令是:
-u用户-pPASS数据库<路径/到/file.sql
这是我的C#代码:
1#
Look at the difference, in my code is not ">", becuse it wont work with it. If I put this char to code, it just show me usage of mysqldump in win cmd. Without it, this code work good.
Now i want to restore database.
the command to restore database is :
-u USER -pPASS DATABASE < path/to/file.sql
Here is my c# codes :
1#
Process.Start(@"C:\Program Files\MySQL\MySQL Server 5.5\bin\mysql.exe", ("-u user -p**** < \"" + path + "\""));
2#
2#
Process myProcess = new Process();
myProcess.StartInfo.FileName = @"C:\Program Files\MySQL\MySQL Server 5.5\bin\mysql.exe";
myProcess.StartInfo.UseShellExecute = false;
myProcess.StartInfo.WorkingDirectory = @"C:\Program Files\MySQL\MySQL Server 5.5\bin\";
myProcess.StartInfo.Arguments = "-u user -p**** PIEx < + "\"" + path + "\"";
myProcess.StartInfo.RedirectStandardInput = false;
myProcess.StartInfo.RedirectStandardOutput = false;
myProcess.Start();
myProcess.WaitForExit();
myProcess.Close();
两种解决方案都不起作用.我认为这是因为此字符"> ",与备份一样的问题,但是在这种情况下,将其删除是行不通的.结果是一样的,它在cmd控制台中记录了mysql的用法.
如果我在cmd中手动执行此命令(移至mysql bin,然后使用参数运行mysql),则工作正常.我尝试将输出/输入/错误重定向到流读取器/写入器-不起作用.尝试使用UseShellExecute = true-不起作用.什么都没用.
帮帮我Thx
Both solutions dont work. I think it is becuse of this char ">", the same problem like backup, but in this case, removing it dont work. The result is same, it write down mysql usages in cmd console.
If I execute this commands manually in cmd (move to mysql bin, then run mysql with arguments), it work fine. I try redirect output/input/error to stream readers/writers - not work. Try to UseShellExecute = true - not work. Nothing works.
Help me guys. Thx
推荐答案
检查此链接
谢谢
-Amit.
Hi,
Check this link
thanks
-Amit.
这篇关于通过C#还原MySQL数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!