如何从 C# 使用 mysql.exe
我需要从 C# 代码调用以下命令: mysql.exe --user=useranme --password=密码 --database=base < C:\backups\data.sql
我使用:
process.StartInfo.UseShellExecute = false;
process.StartInfo.RedirectStandardOutput = true;
process.StartInfo.CreateNoWindow = false;
process.StartInfo.FileName = "mysql.exe";
process.StartInfo.Arguments = "--user=useranme --password=password --database=base < C:\backups\data.sql";
process.OutputDataReceived += new DataReceivedEventHandler(delegate(object sender, DataReceivedEventArgs args)
{
Console.WriteLine(args.Data);
});
process.Start();
process.BeginOutputReadLine();
process.WaitForExit();
但它不能直接进入命令行。我看到正确的部分“< C:\backups\data.sql”未执行。
请帮忙。
I need to call the following command from C# code:
mysql.exe --user=useranme --password=password --database=base < C:\backups\data.sql
I use:
process.StartInfo.UseShellExecute = false;
process.StartInfo.RedirectStandardOutput = true;
process.StartInfo.CreateNoWindow = false;
process.StartInfo.FileName = "mysql.exe";
process.StartInfo.Arguments = "--user=useranme --password=password --database=base < C:\backups\data.sql";
process.OutputDataReceived += new DataReceivedEventHandler(delegate(object sender, DataReceivedEventArgs args)
{
Console.WriteLine(args.Data);
});
process.Start();
process.BeginOutputReadLine();
process.WaitForExit();
But it does not work as a direct entry to the command line. I see that the right part "< C:\backups\data.sql" is not executed.
Help please.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这是因为写作<命令提示符处的 filename 强制 shell 在程序的标准输入中提交文件 c:\backups\data.sql 的内容。传递“<”之后的部分参数中的内容不正确,因为您必须将文件的内容提供给可执行文件的标准输入。为了进行标准输入/标准输出重定向,您可以参考这个问题:
重定向 stdin 和 stdout,其中 stdin 首先关闭
,您将把 data.sql 文件的内容推送到 stdin 流上。
this is because writing < filename at the command prompt force the shell to submit the content of the file c:\backups\data.sql in the standard input of your program. Passing the part after the '<' in the argument is not correct because you have to feed the stdin of your executable whith the content of the file.In order to do stdin/stdout redirection you can refer this question:
Redirecting stdin and stdout where stdin closes first
and you will push the content of your data.sql file onto the stdin stream.
只是我注意到的一点是,您的数据库是否不名为
data.sql
而是以其他方式命名,其中第一个字母与某些 转义序列?您需要转义路径中的所有
\
字符,否则 C# 编译器会将它们视为转义序列。在您的代码中,
"C:\backups"
实际上变为C:□ackups
。您需要在字符串前加上
@
前缀,告诉编译器不要使用转义序列,或者在\
前加上另一个\
前缀:然而,它只是可能您只是在此处输入了一些路径,并且您的代码没有此错误。
Just something I noticed, could it be that your database is not called
data.sql
but somehow else, where first letter matches some escape sequence?You need to escape all
\
characters in path or C# compiler will treat them as escape sequences.In your code,
"C:\backups"
actually becomesC:□ackups
.You either need to prefix string with
@
which tells compiler to not use escape sequences, or else prefix\
with another\
:However it is just as possible that you just typed some path here and your code doesn't have this mistake.
请参阅 SchlaWiener 的答案:
问题,使用 c# 在 cmd 中执行命令
这对我有用! :)
See the answer by SchlaWiener here:
Problem, executing commands in cmd using c#
It worked for me! :)