ExecuteNonQuery () return value in C # attention point analysis

Source: Internet
Author: User

The example in this article describes the ExecuteNonQuery () return value note point in C #. It has some reference value for C # database program design. Share it for everyone's reference. The specific analysis is as follows:

First, when querying a table for data, we usually use ExecuteNonQuery () and determine whether the data exists or not by judging if the value is greater than the limit. The results and I imagined very inconsistent, debugging only found that the result of the return after execution is-1, for which I do not understand, looked back to the following information, as shown below:

The SqlCommand.ExecuteNonQuery method executes Transact-SQL statements on the connection and returns the number of rows affected.

Note:

You can use ExecuteNonQuery to perform directory operations, such as querying the structure of a database or creating database objects such as tables, or by executing an UPDATE, INSERT, or DELETE statement to change data in a database without using a dataset.

Although ExecuteNonQuery does not return any rows, any output parameters or return values that are mapped to parameters are populated with data. For UPDATE, INSERT, and DELETE statements, the return value is the number of rows affected by the command. For all other types of statements, the return value is-1. If a rollback occurs, the return value is also-1

After reading to understand, the original SELECT statement is not suitable for ExecuteNonQuery () method, the original is so, the method used wrong! Now deliberately write it down, I hope friends do not make a similar mistake!

public int ExecuteNonQuery ();

return value

The number of rows affected.

Realize

Idbcommand.executenonquery

Note:

You can use ExecuteNonQuery to perform directory operations such as querying the structure of a database or creating database objects such as tables, or by executing an UPDATE, INSERT, or DELETE statement to change data in a database without using a dataset.


Although ExecuteNonQuery does not return any rows, any output parameters or return values that are mapped to parameters are populated with data.


For UPDATE, INSERT, and DELETE statements, the return value is the number of rows affected by the command. For all other types of statements, the return value is-1.

public void Createmysqlcommand (string myexecutequery, SqlConnection myconnection) {   SqlCommand mycommand = new SqlCommand (Myexecutequery, myconnection);   MyCommand.Connection.Open ();   Mycommand.executenonquery ();   Myconnection.close ();}

It is believed that this article has certain reference value for the C # program design of everybody.

In addition to the Declaration, Running GuestArticles are original, reproduced please link to the form of the address of this article
ExecuteNonQuery () return value in C # attention point analysis

This address: http://www.paobuke.com/develop/c-develop/pbk23578.html






Related content A simple example of using C # to write a Bluetooth communication program in a Windows system C # implementation belongs to its own QQ tool c#6.0 10 features in the application and summary C # making simple multiplayer online instant chat room
C # calendar-style drop-down calculator examples of C # methods for implementing RAR compression and Extracting files C # method of finding all occurrences of elements in a list C # custom processing XML data class instance

ExecuteNonQuery () return value in C # attention point analysis

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.