I execute a huge script that gives an error! Code:

MySqlConnection myConnection = new MySqlConnection(Connect); MySqlScript ScriptExecute = new MySqlScript(myConnection, script); try { ScriptExecute.Execute(); DoScriptsResult = "GOOD"; } catch (MySqlException ex) { // ex.Number - это НЕ Π½ΠΎΠΌΠ΅Ρ€ строки, Π° Π½ΠΎΠΌΠ΅Ρ€ ΠΈΡΠΊΠ»ΡŽΡ‡Π΅Π½ΠΈΡ. Π° Π½ΡƒΠΆΠ΅Π½ Π½ΠΎΠΌΠ΅Ρ€ строки... // ex.Message - это сообщСниС ошибки. DoScriptsResult = "ERROR"; } 

And I need to display the line number in the script that gives an error.

  • If you are given an exhaustive answer, mark it as correct (a daw opposite the selected answer). - Nicolas Chabanovsky ♦

2 answers 2

Try this:

 bool errorHappened = false; int errorLine = -1; MySqlScriptErrorEventHandler handler = (sender, args) => { errorHappened = true; errorLine = args.Line; }; try { ScriptExecute.Error += handler; ScriptExecute.Execute(); DoScriptsResult = "GOOD"; } catch (MySqlException ex) { DoScriptsResult = "ERROR"; } finally { ScriptExecute.Error -= handler; } if (errorHappened) ... 
  • So in general the error does not appear (DoScriptsResult = "GOOD" always) ScriptExecute.Error + = handler; line hinders. Are there any other options? - user189087
  • What does "interfere" mean? Does management come to handler? - VladD
  • The catch (MySqlException ex) is not included in the exception, even if there is an error in the script. If the comment line is ScriptExecute.Error + = handler; that falls. - user189087 1:21 pm
  • DoScriptsResult is ALWAYS = "GOOD"; - user189087
  • Let me still repeat the question: does the control come to the handler? - VladD pm

I redid everything, but I do not receive data on the line (constantly 0). Full code:

 private void doScripts(string[] scripts, string server, string @base, string login, string paswd) { if (scripts == new string[] { }) { MessageBox.Show("Π’Ρ‹Π±Π΅Ρ€Π΅Ρ‚Π΅ скрипт для выполнСния!"); return; } Connect = "default command timeout=0 ; Database=" + @base + "; Data Source=" + server + "; User Id=" + login + "; Password=" + paswd + "; charset=utf8"; MySqlConnection myConnection = new MySqlConnection(Connect); if (this.OpenConnection(myConnection) == true) // ΠŸΡ€ΠΎΠ²Π΅Ρ€ΠΊΠ° ΠΊΠΎΠ½Π½Π΅ΠΊΡ‚Π° { foreach (var item in scripts) { // ВыполняСм скрипт ------------------------------------------------------------------------------------------------- MySqlScript script = new MySqlScript(myConnection, EncodingUTF8orANSI(item)); bool errorHappened = false; bool errorIgnore = false; int errorPosition = 0; //string errorStatement = ""; string errorStatementText = ""; int errorLine = -1; int count = 0; MySqlScriptErrorEventHandler handler = (sender1, args) => { errorHappened = true; errorLine = args.Line; errorIgnore = args.Ignore; errorPosition = args.Position; //errorStatement = args.Statement; errorStatementText = args.StatementText; richTextBox1.AppendText("\n errorPosition: " + errorPosition + // 0 "\n errorStatementText: " + errorStatementText + // "" "\n Line: " + errorLine + // 0 "\n count: " + count + // 0 "\n ERROR: " + args.Exception.Message + // Π²Ρ‹Π΄Π°Π΅Ρ‚ ΠΎΡˆΠΈΠ±ΠΊΡƒ выполнСния, ΠΊΠ°ΠΊ ΠΈ ΠΏΠΎΠ»ΠΎΠΆΠ΅Π½ΠΎ "\n args.Position: " + args.Position); // Π²Ρ‹Π΄Π°Π΅Ρ‚ ΠΎΡˆΠΈΠ±ΠΊΡƒ выполнСния, ΠΊΠ°ΠΊ ΠΈ ΠΏΠΎΠ»ΠΎΠΆΠ΅Π½ΠΎ DoScriptsResult = "ERROR"; }; try { script.Error += handler; //script.Error += new MySqlScriptErrorEventHandler(script_Error); script.ScriptCompleted += new EventHandler(script_ScriptCompleted); script.StatementExecuted += new MySqlStatementExecutedEventHandler(script_StatementExecuted); count = script.Execute(); } catch (MySqlException ex) { DoScriptsResult = "ERROR"; } finally { richTextBox1.AppendText("\n finally"); } if (errorHappened) { richTextBox1.AppendText("\n ERROR"); } private void script_Error(Object sender, MySqlScriptErrorEventArgs args) { richTextBox1.AppendText("\n args.Ignore: " + args.Ignore + // 0 "\n args.Position: " + args.Position + // "" "\n args.StatementText: " + args.StatementText + // 0 "\n args.Line: " + args.Line + // 0 "\n ERROR: " + args.Exception.Message); // Π²Ρ‹Π΄Π°Π΅Ρ‚ ΠΎΡˆΠΈΠ±ΠΊΡƒ выполнСния, ΠΊΠ°ΠΊ ΠΈ ΠΏΠΎΠ»ΠΎΠΆΠ΅Π½ΠΎ } private void script_StatementExecuted(object sender, MySqlScriptEventArgs args) { richTextBox1.AppendText("\n script_StatementExecuted"); } private void script_ScriptCompleted(object sender, EventArgs e) { richTextBox1.AppendText("\n script_ScriptCompleted"); /// EventArgs e will be EventArgs.Empty for this method } 

In response, I get:

  script_StatementExecuted script_StatementExecuted args.Ignore: False args.Position: 0 args.StatementText: args.Line: 0 ERROR: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '`,`assistant_id`) select `id`,`createDatetime`,`createPerson_id`, `modifyDatetim' at line 4 script_ScriptCompleted finally 

Here is the script:

 set foreign_key_checks=0; set sql_safe_updates = 0; insert into Visit (`id`,`createDatetime`,`createPerson_id`, `modifyDatetime`,`modifyPerson_id`,`deleted`,`event_id`,`scene_id`, `date`,`visitType_id`,`person_id`,`isPrimary`,`finance_id`,`service_id`, payStatus`,`assistant_id`) select `id`,`createDatetime`,`createPerson_id`, `modifyDatetime`,`modifyPerson_id`,`deleted`,`event_id`,`scene_id`,`date`, `visitType_id`,`person_id`,`isPrimary`,`finance_id`,`service_id`,`payStatus`, `assistant_id` from base2.Visit; set foreign_key_checks=1; 

It is necessary to get the 5th line, there is an error in the request. (quotes are not in the 8th line)