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