0

I want to run all commands programmatically and the commands are like:

string mysql ="C:\Program Files\MySQL\MySQL Server 5.1\bin"
string command =" mysql.exe -u root -ppassword fabrica < c:/backup.sql";

I want to run these two lines using C#, how can I achieve this?

4
  • 1
    yes process.start("cmd") and how to run these also ? Commented Dec 6, 2012 at 17:11
  • put them in a batch file, and run it. Commented Dec 6, 2012 at 17:13
  • Process.Start("cmd", "/c "+"\"C:\Program Files\MySQL\MySQL Server 5.1\bin\mysql.exe\""+" -u root -ppassword fabrica < c:/backup.sql") Commented Dec 6, 2012 at 17:14
  • May I suggest, CLI may not be the most productive way to interact with your database. Commented Dec 6, 2012 at 17:20

1 Answer 1

3

EDITED : Now i know what you want to do exactly

Here is a code to make it in a method

string binary = @"C:\MySQL\MySQL Server 5.0\bin\mysqldump.exe"
string arguments = @"-uroot -ppassword sample"
ProcessStartInfo PSI = new System.Diagnostics.ProcessStartInfo(binary, arguments);
PSI.RedirectStandardInput = true;
PSI.RedirectStandardOutput = true;
PSI.RedirectStandardError = true;
PSI.UseShellExecute = false;
Process p = System.Diagnostics.Process.Start(PSI);
Encoding encoding = p.StandardOutput.CurrentEncoding;
System.IO.StreamWriter SW = new StreamWriter(@"c:\backup.sql", false, encoding);
p.WaitOnExit();
string output = p.StandardOutput.ReadToEnd()
SW.Write(output)
SW.Close();

Good luck!

Sign up to request clarification or add additional context in comments.

4 Comments

this line not executing pr.StartInfo.Arguments = @"mysql.exe -u root -ppassword fabrica < c:/backup.sql";
no any exception . after executing this command prompt stop on "C:\Program Files\MySQL\MySQL Server 5.1\bin"; next line not executing
do you want to dump a MySQL database? using c#
and dump backup i think we cant restore using Msql application

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.