cmd执行mssql脚本或者执行mysql脚本

 1   private static int ExecuteMSSql(DbInfo db, string sqlPath)
 2         {
 3             Console.WriteLine("=================================================================");
 4             Console.WriteLine("==========当前执行的mssql脚本:"+sqlPath);
 5             Console.WriteLine("=================================================================");
 6             if (db != null)
 7             {
 8                 System.Diagnostics.Process p = new System.Diagnostics.Process();
 9                 p.StartInfo.FileName = "cmd.exe";//要执行的程序名称
10                 p.StartInfo.UseShellExecute = false;
11                 p.StartInfo.RedirectStandardInput = true;//可能接受来自调用程序的输入信息
12                 p.StartInfo.RedirectStandardOutput = true;//由调用程序获取输出信息
13                 p.StartInfo.CreateNoWindow = true;//不显示程序窗口
14                 p.Start();
15                 string Command = string.Format(@"sqlcmd -U{0} -P{1} -S{2} -d{3} -b -i {4}", db.User, db.Password, db.Server, db.Database, sqlPath);
16                 p.StandardInput.WriteLine(Command);  //向CMD窗口发送输入信息:             
17                 p.StandardInput.WriteLine("exit");
18                 p.WaitForExit();
19                 string sOutput = p.StandardOutput.ReadToEnd();//获取CMD窗口的输出信息,必须放在WaitForExit后面
20                 Console.WriteLine(sOutput);//输出CMD执行的返回信息,
21                 int rt = p.ExitCode;//CMD返回值,成功为0失败为1 必须放在WaitForExit后面.在执行的Command里面必然加上-b,否则一直返回O       
22                 p.Close();
23                 Console.WriteLine(sOutput);
24                 if (rt == 1)
25                 {
26                     Console.WriteLine("=================================================================");
27                     Console.WriteLine("==========返回值:-9,mysql脚本执行出错,没有返回执行成功结果");
28                     Console.WriteLine("==========mysql脚本:"+sqlPath);
29                     Console.WriteLine("=================================================================");
30                     return -9;//脚本错误
31                 }
32                 else
33                 { return 0; }
34             }
35             else
36             {
37                 Console.WriteLine("=================================================================");
38                 Console.WriteLine("==========返回值:-8,mysql数据库连接读取失败,为Null");
39                 Console.WriteLine("=================================================================");
40                 return -8;//数据库连接读取失败
41             }
42 
43         }
44                 
45         private static int ExecuteMySql(DbInfo db, string sqlPath)
46         {
47             Console.WriteLine("=================================================================");
48             Console.WriteLine("==========当前执行的mysql脚本:"+sqlPath);
49             Console.WriteLine("=================================================================");
50             if (db != null)
51             {                
69                 Process myProcess = new Process();
70                 myProcess.StartInfo = new ProcessStartInfo(); 
72                 myProcess.StartInfo.UseShellExecute = false;
73                 myProcess.StartInfo.FileName = "cmd.exe";
74                 string Command = string.Format("/c mysql.exe --host={0} -u{1} -p{2} --default-character-set=utf8 {3} -v -E < "{4}"", db.Server, db.User, db.Password, db.Database, sqlPath);
75                 myProcess.StartInfo.Arguments = Command;
76                 myProcess.Start();
77                 myProcess.WaitForExit();
78                 int rt = myProcess.ExitCode;//CMD返回值,成功为0失败为1 必须放在WaitForExit后面.在执行的Command里面必然加上-b,否则一直返回O
79                 if (rt == 1)
80                 {
81                     Console.WriteLine("=================================================================");
82                     Console.WriteLine("==========返回值:-9,mssql脚本执行出错脚本出错");
83                     Console.WriteLine("==========mssql脚本:"+sqlPath);
84                     Console.WriteLine("=================================================================");
85                     Console.WriteLine("");
86                     return -9;//脚本错误
87                 }
88                 else
89                 { return 0; }
90             }
91             else
92             {
93                 Console.WriteLine("=================================================================");
94                 Console.WriteLine("==========返回值:-8,mssql数据库连接读取失败,为Null");
95                 Console.WriteLine("=================================================================");
96                 return -8;//数据库连接读取失败
97             }
98         }
原文地址:https://www.cnblogs.com/_fyz/p/3435628.html