Simple implementation of SQL scripts in .NET
Zhengzuo
2004-12-25
Seeing the 9CBS community often asks if the SQL script is executed in .NET, the following is the execution of the OSQL implementation script using the C # call CMD.
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.uShellexecute = false;
P.Startinfo.redirectStandardInput = true;
P.Startinfo.RedirectStandardOrdoutput = 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 named after OSQL are as follows:
=====================
Usage: OSQL [-u login id] [-P password]
[-H Hostname] [-e Trusted Connection]
[-d use database name] [-l login timeout] [-t query timeout]
[-h Headers] [-s colseparator] [-w colornwidth]
[-e echo input] [-i enable quoted identifier]]
[-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 infutfile] [-o outputfile]
[-P print statistics] [-b on error batch Abort]
[-X [1] disable commands [and exit with warning]]]
[-Use old isql behavior disables the folowing]
[-? Show syntax summary]
Specific reference
http://www.588188.com/netbook/sqlserver2000/coprompt/cp_osql_1wxl.htm
Or SQL Server 2000 Help Document
The above program is written when I first answer questions in 9CBS, because it is relatively busy, so lazy. For any good suggestions or comments in this article, please leave a message. Zhzuo (Autumn Maple)