There is a method

public void Send(int one, int two) { try { using (MySqlConnection myCon = new MySqlConnection("подключ.")) { MySqlCommand upd = new MySqlCommand("INSERT INTO Numbers VALUES ('one', 'two')", myCon); myCon.Open(); upd.ExecuteNonQuery(); } } catch(Exception ex) { Console.WriteLine(ex.Message); } } 

Please tell me how to check that it was executed (ie, the data was added and the connection did not terminate)?

  • 3
    Well, if there was an exception - did not run - Grundy
  • Suppose I have another method that removes one and two, but it is necessary to execute it only if one and two are already added. That is, I have only one option - put its implementation under upd.ExecuteNonQuery (); ? Or how else can you? - dexploizer
  • the task itself is incomprehensible, maybe just a request change is enough - Grundy
  • dev.mysql.com/doc/refman/5.7/en/… (on the database side) or check the value of affected rows after the first request and set some flag (on the C # side) - free_ze
  • delete from NUMBERS where field1='one' and field2='two' will delete only if one and two have already been added - Igor

4 answers 4

Any exception that occurs in try (for example, an invalid value or no connection) will result in the execution of the code from catch . In your case, this is an error output ... Where you run this method, you just need to write the same try-catch . If an error occurs in using the method, the code from the catch

 try{ send(int one, int two); } catch(Exception ex) { Console.WriteLine(ex.Message); } 
  • Earned completely) Thank you! - dexploizer
  • 3
    C why would it help? The first catch exception is swallowed and the second will not get it. - Qwertiy
  • "the second will not get it" - which second? please explain what you had in mind - dexploizer
  • one
    @YuriGo, while the catch that is now inside Send , Send itself will never throw an exception, therefore wrapping its call in catch useless. - Qwertiy

Remove

 catch(Exception ex) 

and process the necessary exception where it is necessary to check whether the addition was completed.

    Please tell me how to check that it was executed (ie, the data was added and the connection did not terminate)?

    By the value that the ExecuteNonQuery() method returns.

    In the try-catch construct, it would be nice to catch exceptions like MySqlException , since if these are connection problems ( Open() ), then this type of exception will be generated.

    After the connection is established - operate on the connection.State values, it seems to be convenient.

    Accordingly, the code will look like this:

     try { send( .. , .. ); } catch( MySqlException ex ) { // ... } catch( Exception ex ) { // ... } 

      Look at the task from the other side. You are trying in C # to solve the problem that the database should solve. Wrap the INSERT into an explicit transaction (in principle, this is not necessary, but there will be no harm, you can always roll back the changes if something went wrong. MySQL can do this with some version) and wrap it all in a stored procedure. From C # you only need to establish a connection, transfer the procedure call and its parameters. To diagnose that everything went well, return the number from the storage as a return code and find out from it whether everything went right, or some kind of error occurred. I do not argue, .NET in general, and C # in particular, have good support for operations in the database, but all the same, the tasks of the database should be solved by the database.

      • And any of these steps can still fall with the exception ... - Qwertiy
      • @Qwertiy, and for various reasons, I didn’t assume the absence of exception handling, but still this option IMHO is a bit more reliable than sending a bare request, but in case of changes, the procedure code can be corrected and released a bit easier than recompiling the client. - rdorn
      • This may be a server, not a client. - Qwertiy
      • @Qwertiy for the database it is the same client. Yes, and stopping the application server to correct the request curve, the solution is also so-so, users will not say exactly thanks, although of course it all depends, you can download the updated release and restart when there are no active users, if the service is not 24x7. - rdorn
      • Usually the database server and the server application itself are updated simultaneously. - Qwertiy