C # cmd BCP export data

Source: Internet
Author: User

Background requirements: Automatic synchronization of data between applications, requiring high efficiency without human intervention

Technical implementation: C # start cmd, pass the necessary parameters via bcp command, achieve data export

<summary>
CMD, launch the application command
Do not show Command window
</summary>
<param name= "Programexepath" > Specify the full path of the application </param>
<param name= "programparmstr" > Execute command Line arguments </param>
public bool Startcmd (string Programexepath, String programparmstr)
{
BOOL result = FALSE;
Process Mypro = new process ();
Try
{
using (Mypro)
{
If there are spaces in the calling program path, the cmd command fails to execute and can be enclosed in double quotes, where two quotation marks indicate a quotation mark (escape)
String str = "";
if (programexepath.trim () = = "")
str = string. Format (@ "{0} {1}", Programparmstr, "&exit");
Else
str = string. Format (@ "" "{0}" "{1} {2}", Programexepath, Programparmstr, "&exit");

ProcessStartInfo startinfo = new ProcessStartInfo ();
Set the command to be executed
Startinfo.filename = "cmd.exe";
startinfo.arguments = str;
Booting without using the system shell
Startinfo.useshellexecute = false;
REDIRECT Input
Startinfo.redirectstandardinput = true;
REDIRECT Output
Startinfo.redirectstandardoutput = true;
Startinfo.redirectstandarderror = true;
Do not create a window
Startinfo.createnowindow = true;
Mypro.startinfo = StartInfo;
Mypro.start ();

MyPro.StandardInput.WriteLine (str);
MyPro.StandardOutput.ReadToEnd ();
MyPro.StandardInput.AutoFlush = true;
MyPro.StandardOutput.ReadToEnd ();
Mypro.waitforexit ();

result = true;
}
}
catch (Exception er)
{
Mypro.close ();

throw new Exception ("Error 006:" +er. Message);

}
return result;
}

C # cmd BCP export data

Related Article

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.