C# 程序调用osql
通过程序调用osql来执行需要升级的脚本。
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 /// <summary>
2 /// 执行bat文件
3 /// </summary>
4 /// <param name="FilePath"></param>
5 /// <returns></returns>
6 protected bool ExecutiveBat(string FilePath)
7 {
8 bool success = false;
9 try
10 {
11 //System.Diagnostics.Process proc;
12 //proc = System.Diagnostics.Process.Start(FilePath);
13 //proc.WaitForExit();
14 //success = true;
15
16 string createStr = "-E -S . -d Kivii_YL -i" + FilePath;//可信任连接,本地服务器,数据库,sql脚本路径
17 System.Diagnostics.Process pr = new System.Diagnostics.Process();
18 pr.StartInfo.FileName = "osql.exe";//调用osql工具
19 pr.StartInfo.Arguments = createStr;//加入创建字符串
20 pr.StartInfo.UseShellExecute = false;
21 pr.StartInfo.RedirectStandardOutput = true; //重定向输出
22 pr.StartInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;//隐藏输出窗口
23 pr.Start();
24 System.IO.StreamReader sr = pr.StandardOutput;
25 sr.ReadToEnd();
26 pr.WaitForExit();
27 pr.Close();
28 success = true;
29 }
30 catch { success = false; }
31 return success;
32 }
2 /// 执行bat文件
3 /// </summary>
4 /// <param name="FilePath"></param>
5 /// <returns></returns>
6 protected bool ExecutiveBat(string FilePath)
7 {
8 bool success = false;
9 try
10 {
11 //System.Diagnostics.Process proc;
12 //proc = System.Diagnostics.Process.Start(FilePath);
13 //proc.WaitForExit();
14 //success = true;
15
16 string createStr = "-E -S . -d Kivii_YL -i" + FilePath;//可信任连接,本地服务器,数据库,sql脚本路径
17 System.Diagnostics.Process pr = new System.Diagnostics.Process();
18 pr.StartInfo.FileName = "osql.exe";//调用osql工具
19 pr.StartInfo.Arguments = createStr;//加入创建字符串
20 pr.StartInfo.UseShellExecute = false;
21 pr.StartInfo.RedirectStandardOutput = true; //重定向输出
22 pr.StartInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;//隐藏输出窗口
23 pr.Start();
24 System.IO.StreamReader sr = pr.StandardOutput;
25 sr.ReadToEnd();
26 pr.WaitForExit();
27 pr.Close();
28 success = true;
29 }
30 catch { success = false; }
31 return success;
32 }