System.Diagnostics.Process Pro = new System.Diagnostics.Process ();
Pro. Startinfo.filename = "cmd.exe";
Pro. Startinfo.useshellexecute = false;
Pro. Startinfo.redirectstandardinput = true;
Pro. Startinfo.redirectstandardoutput = true;
Pro. Startinfo.createnowindow = true;
Pro. Start ();
Pro. Standardinput.writeline ("net use \\\\202.149.225.121\\ipc$ \" aischina_admin\ "/user:\" administrator\ "");
Pro. Standardinput.writeline ("CD d:\\ program \ \");
Pro. Standardinput.writeline ("dir * *");
Focus: When writing the batch command below, make sure that you exit the CMD window by adding exit at the end of the line,
At the end of the SQL script must also write the exit, indicating exit Sqlplus, or the program will wait until the end of CMD, suspended animation.
string cmd = string. Format (@ "Sqlplus {0}/[email protected] @{1}
Exit ", Aislogical.dbuser, Tb_sqlpath. Text);
Pro. Standardinput.writeline (CMD);
Intercept output stream
StreamReader reader = Pro. StandardOutput;
String line = reader. ReadLine (); Read one line at a time
while (!reader. Endofstream)//non-empty read
{
if (line!= "")
Tb_output. AppendText (line + Environment.NewLine); Environment. NewLine line break
line = reader. ReadLine (). Trim ();
Application.doevents ();
}
////
Pro. WaitForExit ();
Pro. Close ();
2016.1.23 Execute SQL script in program with CMD