因為項目中需要使用BCP命令導出數據庫中數據,然后將導出結果的統計信息輸出到文本中,但是bcp命令對文件夾的操作權限不足,所以就用到了Process類以管理員的身份運行BCP控制台程序,直接上代碼:
Process p = new Process();
try
{
p.StartInfo.FileName = "bcp.exe";
p.StartInfo.RedirectStandardInput = true;
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.RedirectStandardError = true;
p.StartInfo.UseShellExecute = false;
p.StartInfo.Verb = "RunAs";
p.StartInfo.UseShellExecute = false;
//@必須加上,不然特殊字符會被自動過濾掉
p.StartInfo.Arguments = sql;
p.Start();
p.WaitForExit();
p.Close();
}
catch (Exception e)
{
log.writeLog_DB("失敗020", sql + "||||||" + e.Message.ToString());
return false;
}