2016.1.23 通过cmd在程序中执行sql脚本

System.Diagnostics.Process pro = new System.Diagnostics.Process();

            pro.StartInfo.FileName = "cmd.exe";

            pro.StartInfo.UseShellExecute = false;

            pro.StartInfo.RedirectStandardInput = true;

            pro.StartInfo.RedirectStandardOutput = true;

            pro.StartInfo.CreateNoWindow = true;

            pro.Start();

 

            //pro.StandardInput.WriteLine("net use \\\\202.149.225.121\\ipc$ \"AISchina_Admin\" /user:\"administrator\"");

            //pro.StandardInput.WriteLine("cd D:\\程序\\");

            //pro.StandardInput.WriteLine("dir *.*");

 

            //重点:下面写入批处理命令时,一定要在末尾换行加上exit,表示退出cmd窗口,

            //同时sql脚本末尾也必须写上exit,表示退出sqlplus,否则程序会一直等待cmd结束,假死。

            string cmd = string.Format(@"sqlplus {0}/ndp@ndp @{1} 

            exit", AisLogical.dbuser, tb_sqlpath.Text);

            pro.StandardInput.WriteLine(cmd);

           

            // 截取输出流

            StreamReader reader = pro.StandardOutput;

            string line = reader.ReadLine();   // 每次读一行

            while (!reader.EndOfStream)  // 不为空则读取

            {

                if(line!="")

                    tb_output.AppendText(line + Environment.NewLine);  // Environment .NewLine 换行   

                line = reader.ReadLine().Trim();

                Application.DoEvents();

            }

            ////

            pro.WaitForExit();

            pro.Close();

posted on 2016-10-15 17:51  mol1995  阅读(213)  评论(0编辑  收藏  举报

导航