Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use mysql.exe from C#

Tags:

c#

mysql

cmd

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.

like image 288
Jean Louis Avatar asked Nov 18 '25 00:11

Jean Louis


1 Answers

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.

like image 152
Felice Pollano Avatar answered Nov 20 '25 14:11

Felice Pollano



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!