如何使用mysql.exe从C# [英] How to use mysql.exe from C#

查看:140
本文介绍了如何使用mysql.exe从C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要从C#代码调用以下命令:
mysql.exe --user = useranme --password = password --database = base< 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

我使用:

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未执行。

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.

请帮助。

推荐答案

这是因为写<命令提示符处的文件名强制shell提交程序的标准输入中文件c:\backups\data.sql的内容。传递参数后面的'<'之后的部分是不正确的,因为你必须提供可执行文件的stdin和文件的内容。为了做stdin / stdout重定向,你可以参考这个问题:

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:

重定向stdin和stdout其中stdin首先关闭

,您将data.sql文件的内容推送到stdin流。

and you will push the content of your data.sql file onto the stdin stream.

这篇关于如何使用mysql.exe从C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆