1010using System . Diagnostics ;
1111using System . Threading . Tasks ;
1212using System . Windows . Forms ;
13+ using System . Threading ;
14+ using System . Data . SqlTypes ;
15+ using System . Xml . Linq ;
1316
1417namespace RunMultiSqlscript
1518{
@@ -25,9 +28,12 @@ public Form1()
2528 public static string FilePath = $@ "{ Directory . GetCurrentDirectory ( ) } { "\\ " } "; //當前工作路徑
2629 public static string LogPath = $@ "{ FilePath } { "\\ " } { "log" } "; //Log檔放置路徑
2730
31+ //設定Log檔名
32+ public string FileLog = "view_" + String . Format ( "{0:yyyyMMdd}" , DateTime . Now ) + "_" + String . Format ( "{0:HHmmss}" , DateTime . Now ) + ".log" ;
2833
29- //資料庫連線區啟用設定
30- public void ConnectionSettings ( bool Status )
34+
35+ //資料庫連線區啟用設定
36+ public void ConnectionSettings ( bool Status )
3137 {
3238 DBLocation . Enabled = Status ;
3339 DBName . Enabled = Status ;
@@ -40,7 +46,9 @@ public void FolderPathCheckedListSettings(bool Status)
4046 FolderPath . Enabled = Status ;
4147 SelectFolder . Enabled = Status ;
4248 FileCheckedList . Enabled = Status ;
43- }
49+ AllChecked . Enabled = Status ;
50+ AllCheckedClear . Enabled = Status ;
51+ }
4452 //匯出list.sql&執行scripts區域啟用設定
4553 public void ScriptBtnSettings ( bool Status )
4654 {
@@ -88,6 +96,10 @@ public string ContentRead(string path)
8896 }
8997 #endregion
9098
99+ #region 功能
100+ /// <summary>
101+ /// 選擇資料夾
102+ /// </summary>
91103 private void SelectFolder_Click ( object sender , EventArgs e )
92104 {
93105 FolderPath . Clear ( ) ;
@@ -120,7 +132,10 @@ private void SelectFolder_Click(object sender, EventArgs e)
120132 }
121133 }
122134
123- private void FileCheckedList_Click ( object sender , EventArgs e )
135+ /// <summary>
136+ /// 項目清單
137+ /// </summary>
138+ private void FileCheckedList_Click ( object sender , EventArgs e )
124139 {
125140 try
126141 {
@@ -153,8 +168,10 @@ private void FileCheckedList_Click(object sender, EventArgs e)
153168 }
154169 }
155170
156-
157- private void FolderPath_KeyDown ( object sender , KeyEventArgs e )
171+ /// <summary>
172+ /// 資料夾路徑區對應鍵盤按鍵
173+ /// </summary>
174+ private void FolderPath_KeyDown ( object sender , KeyEventArgs e )
158175 {
159176 try
160177 {
@@ -191,6 +208,9 @@ private void FolderPath_KeyDown(object sender, KeyEventArgs e)
191208 }
192209 }
193210
211+ /// <summary>
212+ /// 輸出SQL清單
213+ /// </summary>
194214 private void OutputScripts_Click ( object sender , EventArgs e )
195215 {
196216 SaveFileDialog saveFileDialog = new SaveFileDialog ( ) ;
@@ -204,7 +224,7 @@ private void OutputScripts_Click(object sender, EventArgs e)
204224 {
205225 sqlText += $@ ":r { FolderPath . Text } { "\\ " } { str } { "\n " } "; //寫入sql字串資料
206226 }
207- string FileName = saveFileDialog . FileName ;
227+ string FileName = saveFileDialog . FileName + saveFileDialog . Filter ; //檔名+副檔名
208228
209229 // 判斷檔案是否存在
210230 if ( File . Exists ( FileName ) )
@@ -217,6 +237,9 @@ private void OutputScripts_Click(object sender, EventArgs e)
217237 }
218238 }
219239
240+ /// <summary>
241+ /// 資料庫連線
242+ /// </summary>
220243 private void DBConnect_Click ( object sender , EventArgs e )
221244 {
222245 string ConnectionString = $@ "Data Source={ DBLocation . Text } ;Initial Catalog={ DBName . Text } ;Persist Security Info=True;User Id={ UserName . Text } ;Password={ password . Text } ";
@@ -289,6 +312,9 @@ private void DBConnect_Click(object sender, EventArgs e)
289312 }
290313 }
291314
315+ /// <summary>
316+ /// 執行資料庫SQL更新
317+ /// </summary>
292318 private void RunScript_Click ( object sender , EventArgs e )
293319 {
294320 string sqlText = "" ;
@@ -309,28 +335,7 @@ private void RunScript_Click(object sender, EventArgs e)
309335
310336 //儲存清單.sql
311337 ContentWrite ( $@ "{ FilePath } { "list.sql" } ", sqlText ) ;
312- //讀取 一次執行資料夾內所有的Script 文字
313- string readtext = ContentRead ( $@ "{ FilePath } { "sample.bat" } ") ;
314-
315- //設定bat檔內的資練庫連線資訊
316- readtext = readtext . Replace ( "dbIp=*" , $@ "dbIp={ DBLocation . Text } ") ;
317- readtext = readtext . Replace ( "dbName=*" , $@ "dbName={ DBName . Text } ") ;
318- readtext = readtext . Replace ( "dbUsrAcc=*" , $@ "dbUsrAcc={ UserName . Text } ") ;
319- readtext = readtext . Replace ( "dbUsrPwd=*" , $@ "dbUsrPwd={ password . Text } ") ;
320- readtext = readtext . Replace ( "batchFilePath=\" \" " , $@ "batchFilePath=""{ FilePath } """);
321- readtext = readtext.Replace(" dbSqlFilePath= \" \" ", $@ "dbSqlFilePath=""{ FilePath } { "list.sql" } """);
322338
323- string FileName = $@" { FilePath} { "RunScripts.bat" } ";
324-
325- // 判斷檔案是否存在
326- if ( File . Exists ( FileName ) )
327- {
328- //清空文件
329- ContentClear ( FileName ) ;
330- }
331-
332- //寫入欲執行bat
333- ContentWrite ( FileName , readtext ) ;
334339 DialogResult result = MessageBox . Show ( "是否執行勾選的script?" , "提示" , MessageBoxButtons . YesNo ) ;
335340 if ( result == DialogResult . Yes )
336341 {
@@ -339,11 +344,18 @@ private void RunScript_Click(object sender, EventArgs e)
339344 {
340345 Directory . CreateDirectory ( "log" ) ;
341346 }
342- //開始執行批次檔
343- ProcessStartInfo process = new ProcessStartInfo ( ) ;
344- process . FileName = "RunScripts.bat" ;
345- process . WorkingDirectory = FilePath ;
346- Process . Start ( process ) ;
347+
348+ //設定sqlcmd參數
349+ string argument = $@ " -S { DBLocation . Text } -d { DBName . Text } -U { UserName . Text } -P { password . Text } -i ""{ FilePath } { "list.sql" } "" -o ""{ FilePath } { "log" } { "\\ " } { FileLog } """;
350+ //開始執行sqlcmd
351+ ProcessStartInfo process = new ProcessStartInfo(" sqlcmd", argument) ;
352+ process . UseShellExecute = false ;
353+ process . CreateNoWindow = true ;
354+ process . WindowStyle = ProcessWindowStyle . Hidden ;
355+ process . RedirectStandardOutput = true ;
356+ Process proc = new Process ( ) ;
357+ proc . StartInfo = process ;
358+ proc . Start ( ) ;
347359 MessageBox . Show ( $@ "已放置執行紀錄於.\log") ;
348360 }
349361 }
@@ -353,11 +365,17 @@ private void RunScript_Click(object sender, EventArgs e)
353365 }
354366 }
355367
368+ /// <summary>
369+ /// 項目更動
370+ /// </summary>
356371 private void FileCheckedList_SelectedIndexChanged ( object sender , EventArgs e )
357372 {
358373 ScriptBtnLock ( ) ; //右側script生成&執行區域啟用/禁用
359374 }
360375
376+ /// <summary>
377+ /// 全部選取
378+ /// </summary>
361379 private void AllChecked_Click ( object sender , EventArgs e )
362380 {
363381 //選取全部列表項目
@@ -368,7 +386,10 @@ private void AllChecked_Click(object sender, EventArgs e)
368386 ScriptBtnLock ( ) ; //右側script生成&執行區域啟用/禁用
369387 }
370388
371- private void AllCheckedClear_Click ( object sender , EventArgs e )
389+ /// <summary>
390+ /// 取消全選
391+ /// </summary>
392+ private void AllCheckedClear_Click ( object sender , EventArgs e )
372393 {
373394 //取消選取全部列表項目
374395 for ( int i = 0 ; i < FileCheckedList . Items . Count ; i ++ )
@@ -377,8 +398,11 @@ private void AllCheckedClear_Click(object sender, EventArgs e)
377398 }
378399 ScriptBtnLock ( ) ; //右側script生成&執行區域啟用/禁用
379400 }
380- //右側script生成&執行區域啟用/禁用
381- public void ScriptBtnLock ( )
401+
402+ /// <summary>
403+ /// 右側script生成&執行區域啟用/禁用
404+ /// </summary>
405+ public void ScriptBtnLock ( )
382406 {
383407 if ( FileCheckedList . CheckedItems . Count > 0 )
384408 {
@@ -391,5 +415,6 @@ public void ScriptBtnLock()
391415 ScriptBtnSettings ( false ) ;
392416 }
393417 }
394- }
418+ #endregion
419+ }
395420}
0 commit comments