Using system. data. the sqlclient namespace can only execute one SQL statement or multiple Content in the middle; separate, cannot execute similar commands, is there a way to execute some batch commands like a query analyzer, the answer is to execute it through osql.
Detailed usage of osql: http://technet.microsoft.com/zh-cn/library/ms162806.aspx
Call through the local process: /**/ /// <Summary>
///Execute osql commands
/// </Summary>
Public Static Void Excuteosqlcmd ( String CMD, String Errorfile, String Filepath)
{
System. Diagnostics. PROCESS p = New System. Diagnostics. Process ();
P. startinfo. filename = " Osql " ;
P. startinfo. Arguments = CMD;
P. startinfo. useshellexecute = False ;
P. startinfo. redirectstandarderror = True ;
P. startinfo. createnowindow = True ;
P. Start ();
P. waitforexit ();
// P. Close ();
Int Exitcode = P. exitcode;
If (Exitcode ! = 0 )
{
Streamreader SR = New Streamreader (errorfile, system. Text. encoding. Default );
String All = Sr. readtoend ();
Sr. Close ();
// Excuteothercmd (string. Format ("Notepad \" {0} \ "", errorfile ));
Throw New Stepexception (all );
}
}
call:
string comm = string. format ("-U {5}-P {0}-s {1}-d {2}-B-e-I \" {3} \ "-O \" {4 }\"", pass,
Conn. datasource,
Conn. database,
filepath, errorfile, user);
process. excuteosqlcmd (Comm, errorfile, filepath); // directly execute an SQL file with the path filepath