using System;using System.Diagnostics;using System.Windows.Forms;namespace WindowsFormsapplication1{ public partial class Form1 : Form { public Form1() { InitializeComponent(); } PRivate void button1_Click(object sender, EventArgs e) { //參數 string[] args = new string[5]; args[0] = "-U " + txtUserName.Text; //用戶名 args[1] = "-P " + txtPassWord.Text; //用戶密碼 args[2] = "-S " + txtServer.Text; //服務器 args[3] = "-d " + txtDatabase.Text; //數據庫 args[4] = "-i " + AppDomain.CurrentDomain.BaseDirectory + "Install.sql"; //sql腳本路徑 //執行 textBox1.Text = CommandLine("osql.exe", args); } #region 調用命令行工具 /// <summary> /// 調用命令行工具 /// </summary> /// <param name="name">命令行工具名稱</param> /// <param name="args">可選命令行參數</param> /// <remarks>注意:所有命令行工具都必須保存于system32文件夾中</remarks> /// <returns></returns> private string CommandLine(string name, params string[] args) { return CommandLine(name, "", args); } /// <summary> /// 調用命令行工具 /// </summary> /// <param name="name">命令行工具名稱</param> /// <param name="workingDirectory">設置工作目錄</param> /// <param name="args">可選命令行參數</param> /// <remarks>注意:所有命令行工具都必須保存于system32文件夾中</remarks> /// <returns></returns> private string CommandLine(string name, string workingDirectory, params string[] args) { string returnValue = ""; using (Process commandline = new Process()) { try { commandline.StartInfo.UseShellExecute = false; commandline.StartInfo.CreateNoWindow = true; commandline.StartInfo.WindowStyle = ProcessWindowStyle.Hidden; commandline.StartInfo.RedirectStandardOutput = true; commandline.StartInfo.FileName = name; commandline.StartInfo.WorkingDirectory = workingDirectory; //添加命令行參數 if (args.Length > 0) commandline.StartInfo.Arguments = string.Join(" ", args); commandline.Start(); commandline.WaitForExit(); returnValue = commandline.StandardOutput.ReadToEnd(); commandline.Close(); } catch { commandline.Dispose(); throw; } } return returnValue; } #endregion }}
新聞熱點
疑難解答
圖片精選