Why does the sqlcommand. executenonquery () method return-1?

Source: Internet
Author: User
Why does the sqlcommand. executenonquery () method return-1?

I don't know if you have encountered the case of returning-1 when using the executenonquery method. The number of affected rows returned by the executenonquery method during addition, deletion, and modification. Then how does one return-1? After checking some information, I finally learned why-1 was returned.

When adding, deleting, and modifying data, the database can calculate the number of affected rows only if nocount is disabled, that is, set nocount off. By default, it is disabled, that is, we can obtain the number of affected rows. You can test it as follows (there is a difference between adding and not adding set nocount on ):

Sqlcommand cmd =   New Sqlcommand ( " Set nocount on; insert into test values ('test ') " , Con );
Int Rows = Cmd. executenonquery ();

Some people just said, I just call a simple stored procedure. How can I still return-1? This is caused by Visual Studio.

When we write a stored procedure in Visual Studio, it is added with set nocount on by default. In this way, the number of rows that are always unaffected when the stored procedure is called is returned-1. However, I tested it in Visual Studio 2008 team suit and the problem does not exist. Maybe the previous version has such a problem (not tested). pay more attention to this problem.

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.