My tangled SQL returns the number of rows affected and the question of whether the judgment was successful

Source: Internet
Author: User

For example: deletesqlwhere= "Delete TableA where ID in (1,2,3,4,5)";

           Try            {                inti =sqlhelper.executenonquery (CommandType.Text, Deletesqlwhere); if(I >0)                {                    return true; }            }            Catch(Exception ex) {Logmsg.writelog (ex).                ToString ()); return false; }            return false;

If in ID all exists, in general, the result will be three kinds of situation:

Returns 0, indicating that TableA does not have an in ID, so execution fails.

Returns 5, indicating a successful deletion of 5 data.

An exception that cannot be removed due to a table constraint or a primary foreign key association. There is also a syntax error.

If this is the case, the above code is judged to be correct. I suspect that there is not a situation, such as "Delete TableA where ID in (1,2,3,4,5)", Assuming that the data exists in and there is no main foreign key association, such as constraints, will not occur a part of the success of the execution, And the other part of the execution failed?

If that's the case, ADO. The results of SQL execution are not strictly judged on net. Because the above code is meant to be: these 5 pieces of data are either all deleted, or all have not been deleted, at this time i>0 can not express this meaning.

I do not know, SQL implementation mechanism is how, without in-depth study of the database, I basically stay in the phase of additions and deletions. So I don't know if a statement like the one above is either a success or a failure, the result of a similar transaction.

My inclination should be either success or failure.

Otherwise, when judging, i== want to execute the number of data, so it is too troublesome. And many businesses do not know the number of data to execute, at this time before the update and DELETE statements to find out how many data, it is more trouble, a lot of problems.

I take the dead Q group to ask this question, the result is just despised several times, the reason also said a lot of, then is oneself to experience bar such words. But the egg, I do not know who to ask, and do not know the experts, although this is a very humble question.

-------------------------------------------------------------------------------------

Well, one thing is certain, in the SQL statement parameter of the C # execution method, write two SQL statements, such as "Update TableA set aid=2 where Id=3;delete TableB where id=3;". Returns the number of affected rows >0 judged to be successful, is obviously wrong.

It is recommended to use the transaction, but when the transaction can be combined with two statements to write, but the judgment of the time i==2 can, or rollback.

My tangled SQL returns the number of rows affected and the question of whether the judgment was successful

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.