在c#中调用windows脚本,可以增加很多灵活性和复用性。参考网上的一些文章,总结一下
1。首先书写脚本:
        ///
        /// 创建存储过程,如果没有Killspid则创建
        ///

        ///

        public static bool createKillspid()
        {
            string[] sqlLines = {
                "create proc p_killspid ",
                "@dbname varchar(200) --要关闭进程的数据库名 ",
                "as ",
                "declare @sql nvarchar(500) ",
                "declare @spid nvarchar(20) ",
                "declare #tb cursor for ",
                "select spid=cast(spid as varchar(20)) from master..sysprocesses where dbid=db_id(@dbname) ",
                "open #tb ",
                "fetch next from #tb into @spid ",
                "while @@fetch_status=0 ",
                "begin ",
                "exec('kill '+@spid) ",
                "fetch next from #tb into @spid ",
                "end ",
                "close #tb ",
                "deallocate #tb ",
                 "go ",
                    };
            File.WriteAllLines("killspid.sql", sqlLines);
            File.WriteAllText("ExecSQL.bat", "CALL \"C:\\Program Files\\Microsoft SQL Server\\90\\Tools\\Binn\\SQLCMD\" -i killspid.sql ");
 
//调用脚本
            System.Diagnostics.Process proc = new System.Diagnostics.Process();
            proc.StartInfo.FileName = ".\\ExecSQL.bat";
            proc.StartInfo.Arguments = "";
            proc.StartInfo.UseShellExecute = false;
            proc.Start();
//删掉脚本,这样看起来就像运行了某个程序一样,不至于太简陋
            if (File.Exists(".\\ExecSQL.bat"))
                File.Delete(".\\ExecSQL.bat");
            if (File.Exists(".\\killspid.sql"))
                File.Delete(".\\killspid.sql");     
            return true;
        }
2。调用脚本或者运行程序的方法
方法1:直接调用
            System.Diagnostics.Process proc = new System.Diagnostics.Process();
            proc.StartInfo.FileName = ".\\ExecSQL.bat";
            proc.StartInfo.Arguments = "";
            proc.StartInfo.UseShellExecute = false;
            proc.Start();

方法2:
使用MS的Windows Script Control(待续。。。。。。)
string  scr  =   " function hello(){var WshShell = new ActiveXObject(\ " WScript.Shell\ " ); "
+ " var code = \ " WScript hello.js\ " ; "
+ " WshShell.Exec(code);} " ;
MSScriptControl.ScriptControl sc 
=   new  ScriptControl();
sc.Language 
=   " JScript " ;
sc.AllowUI 
=   true ;
sc.AddCode(scr);
object [] parameters  =   new  Object[ 0 ];
sc.Run(
" hello " , ref  parameters);