@@ -60,24 +60,24 @@ private void btnRunScript_Click(object sender, EventArgs e)
6060 if ( ! TestConnectionString ( strConnectionString ) )
6161 return ;
6262
63- short sLinesPerBatch = 0 ;
63+ long sLinesPerBatch = 0 ;
6464 long lStartingLine = 0 ;
6565
66- if ( ! short . TryParse ( strLinesPerBatch , out sLinesPerBatch ) )
66+ if ( ! long . TryParse ( strLinesPerBatch , out sLinesPerBatch ) )
6767 {
6868 MessageBox . Show ( "Invalid value on the lines per batch field!" , "Invalid value!" , MessageBoxButtons . OK , MessageBoxIcon . Exclamation ) ;
6969 return ;
7070 }
7171
72- if ( sLinesPerBatch <= 0 )
72+ if ( sLinesPerBatch < 0 )
7373 {
7474 MessageBox . Show ( "Invalid value on the lines per batch field! Value must be positive!" , "Invalid value!" , MessageBoxButtons . OK , MessageBoxIcon . Exclamation ) ;
7575 return ;
7676 }
7777
78- if ( sLinesPerBatch > 5000 )
78+ if ( sLinesPerBatch > 500000 )
7979 {
80- MessageBox . Show ( "Invalid value on the lines per batch field! Value is limited to 5000 ." , "Invalid value!" , MessageBoxButtons . OK , MessageBoxIcon . Exclamation ) ;
80+ MessageBox . Show ( "Invalid value on the lines per batch field! Value is limited to 500000 ." , "Invalid value!" , MessageBoxButtons . OK , MessageBoxIcon . Exclamation ) ;
8181 return ;
8282 }
8383
@@ -232,6 +232,7 @@ public void ControlScreen(bool bEnable)
232232 txtStartingLine . Enabled = bEnable ;
233233 chkStopOnError . Enabled = bEnable ;
234234 btnStop . Enabled = ! bEnable ;
235+ chkLogErrors . Enabled = bEnable ;
235236 Cursor . Current = bEnable ? Cursors . Default : Cursors . WaitCursor ;
236237 this . UseWaitCursor = ! bEnable ;
237238 }
@@ -254,17 +255,17 @@ public void UpdateProgressBar(int percent)
254255 }
255256 }
256257
257- private bool RunScript ( string sourceScript , string connectionString , short qtyMaxLines , long startLine )
258+ private bool RunScript ( string sourceScript , string connectionString , long qtyMaxLines , long startLine )
258259 {
259260 SqlConnection conn = new SqlConnection ( connectionString ) ;
260261 conn . Open ( ) ;
261262
262263 // Config
263264 string line ;
264- string errorFile = sourceScript + ".error" ;
265+ string errorFile = sourceScript + ".[linenumber]. error" ;
265266 long qtyErrorLines = 0 ;
266267 long qtyLines = 0 ;
267- short control = 0 ;
268+ long control = 0 ;
268269 Stopwatch watchtotal = Stopwatch . StartNew ( ) ;
269270
270271 long totalLines = CountLines ( sourceScript ) ;
@@ -297,31 +298,35 @@ private bool RunScript(string sourceScript, string connectionString, short qtyMa
297298 sb . AppendLine ( line ) ;
298299 }
299300
300- if ( control >= qtyMaxLines || isGO )
301+ if ( ( control >= qtyMaxLines && qtyMaxLines > 0 ) || isGO )
301302 {
302- using ( SqlCommand comm = new SqlCommand ( sb . ToString ( ) , conn ) )
303+ if ( sb . Length > 0 )
303304 {
304- comm . CommandTimeout = 3600 ; // 1 hour
305- try
305+ using ( SqlCommand comm = new SqlCommand ( sb . ToString ( ) , conn ) )
306306 {
307- comm . ExecuteScalar ( ) ;
308- }
309- catch ( Exception ex )
310- {
311- if ( LogErrors )
307+ comm . CommandTimeout = 3600 ; // 1 hour
308+ try
309+ {
310+ comm . ExecuteScalar ( ) ;
311+ }
312+ catch ( Exception ex )
312313 {
313- string errormsg = "/* The error was: \n " + ex . Message + "\n */" ;
314- // Save the chuncks that could not be processed
315- File . AppendAllText ( errorFile , sb . ToString ( ) + Environment . NewLine ) ;
316- File . AppendAllText ( errorFile , errormsg + Environment . NewLine ) ;
314+ if ( LogErrors )
315+ {
316+ string errormsg = "/* The error was: \n " + ex . Message + "\n */" ;
317+ // Save the chuncks that could not be processed
318+ File . AppendAllText ( errorFile . Replace ( "[linenumber]" , qtyLines . ToString ( ) ) , sb . ToString ( ) + Environment . NewLine ) ;
319+ File . AppendAllText ( errorFile . Replace ( "[linenumber]" , qtyLines . ToString ( ) ) , errormsg + Environment . NewLine ) ;
320+ }
321+ qtyErrorLines ++ ;
322+ if ( StopOnError )
323+ AllowToRun = false ;
317324 }
318- qtyErrorLines ++ ;
319- if ( StopOnError )
320- AllowToRun = false ;
325+ control = 0 ;
326+ sb . Clear ( ) ;
321327 }
322- control = 0 ;
323- sb . Clear ( ) ;
324328 }
329+
325330 // Update screen
326331 double percentComplete = ( qtyLines . ToDouble ( ) / totalLines . ToDouble ( ) ) * 100.0 ;
327332 long ticksLeft = Convert . ToInt64 ( ( totalLines . ToDouble ( ) - qtyLines . ToDouble ( ) ) * ( watch . ElapsedTicks / qtyLines . ToDouble ( ) ) ) ;
@@ -341,7 +346,7 @@ private bool RunScript(string sourceScript, string connectionString, short qtyMa
341346 }
342347
343348 // Finished reading file. Check if there's still lines to run
344- if ( control > 0 && AllowToRun )
349+ if ( control > 0 && AllowToRun && sb . Length > 0 )
345350 {
346351 using ( SqlCommand comm = new SqlCommand ( sb . ToString ( ) , conn ) )
347352 {
@@ -356,8 +361,8 @@ private bool RunScript(string sourceScript, string connectionString, short qtyMa
356361 {
357362 string errormsg = "/* The error was: \n " + ex . Message + "\n */" ;
358363 // Save the chuncks that could not be processed
359- File . AppendAllText ( errorFile , sb . ToString ( ) + Environment . NewLine ) ;
360- File . AppendAllText ( errorFile , errormsg + Environment . NewLine ) ;
364+ File . AppendAllText ( errorFile . Replace ( "[linenumber]" , qtyLines . ToString ( ) ) , sb . ToString ( ) + Environment . NewLine ) ;
365+ File . AppendAllText ( errorFile . Replace ( "[linenumber]" , qtyLines . ToString ( ) ) , errormsg + Environment . NewLine ) ;
361366 }
362367 qtyErrorLines ++ ;
363368 if ( StopOnError )
@@ -377,7 +382,7 @@ private bool RunScript(string sourceScript, string connectionString, short qtyMa
377382
378383 if ( qtyErrorLines > 0 && AllowToRun )
379384 {
380- string errorMessage = string . Format ( "{0} lines were not loaded. Please check the error file {1}" , qtyErrorLines , errorFile ) ;
385+ string errorMessage = string . Format ( "{0} lines were not loaded. Please check the error files {1}" , qtyErrorLines , errorFile ) ;
381386 MessageBox . Show ( errorMessage , "Error" , MessageBoxButtons . OK , MessageBoxIcon . Warning ) ;
382387 }
383388
@@ -408,6 +413,7 @@ private long CountLines(string inputFileFullPath)
408413 }
409414 }
410415 }
416+
411417 return lineCount ;
412418 }
413419
0 commit comments