[Go] Use C # call CMD to execute SQL script

Source: Internet
Author: User

This article turns from: 1822669

The following is a call to CMD using C # to perform the execution of the osql implementation script.



Using System;

Using System.Data;

Using System.Collections;

Using System.Xml;

Using System.IO;

Using System.Text;

Using System.Diagnostics;



Namespace ZZ

{

public class Zzconsole

{

[STAThread]

static void Main (string[] args)

{

String sqlquery = "osql.exe/usa/p123/s192.192.132.229/dnorthwind/i yoursql.sql";

String strrst = Execommand (sqlquery);

Console.WriteLine (Strrst);

Console.ReadLine ();

}



public static string Execommand (String commandtext)

{

Process P = new process ();

p.StartInfo.FileName = "cmd.exe";

P.startinfo.useshellexecute = false;

P.startinfo.redirectstandardinput = true;

P.startinfo.redirectstandardoutput = true;

P.startinfo.redirectstandarderror = true;

P.startinfo.createnowindow = true;

string stroutput = null;

Try

{

P.start ();

P.standardinput.writeline (CommandText);

P.standardinput.writeline ("Exit");

Stroutput = P.standardoutput.readtoend ();

p.WaitForExit ();

P.close ();

}

catch (Exception e)

{

Stroutput = E.message;

}

return stroutput;

}

}

}



The parameters for osql are named as follows:

=====================



Usage: osql [-u login ID] [-p password]

[-s server] [-H hostname] [-E Trusted Connection]

[-D use database name] [-L Login Timeout] [-t query timeout]

[-H headers] [-S Colseparator] [-W ColumnWidth]

[-A packetsize] [-e echo input] [-I Enable Quoted Identifiers]

[-l list Servers] [-C Cmdend] [-D ODBC DSN name]

[-Q "CmdLine query"] [-Q "CmdLine query" and exit]

[-N Remove numbering] [-M ERRORLEVEL]

[-R msgs to stderr] [-V Severitylevel]

[-I. Inputfile] [-O outputfile]

[-P print statistics] [-B On Error batch abort]

[-x[1] Disable commands [and exit with warning]]

[-O use old ISQL behavior disables the following]

[-? show syntax summary]

Specific reference

Http://www.588188.com/netbook/sqlserver2000/coprompt/cp_osql_1wxl.htm

or SQL Server 2000 Help documentation

trackback:http://tb.blog.csdn.net/trackback.aspx?postid=1769232

[Go] Use C # call CMD to execute SQL script

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.