2016.1.23 Execute SQL script in program with CMD

Source: Internet
Author: User
Tags sqlplus

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.