通过System.Data.SqlClient命名空间只能执行单条SQL,或多个内容中间用;分开,不能执行类似等命令,有没有一种方法能类似查询分析器一样执行一些批命令呢,答案是通过OSQL来执行。
OSQL详细用法: http://technet.microsoft.com/zh-cn/library/ms162806.aspx
通地进程来调用:
///
<summary>
///
执行OSQL命令
///
</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);
}
}
调用如下:
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);//直接执行一个SQL文件,路径为FilePath
OSQL详细用法: http://technet.microsoft.com/zh-cn/library/ms162806.aspx
通地进程来调用:



























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);//直接执行一个SQL文件,路径为FilePath