Ado. ExecuteNonQuery, Excutereader, and ExecuteScalar Methods for command objects in net

Source: Internet
Author: User

1, ExecuteNonQuery method.
The method performs an update operation, that is, an operation related to a statement such as update, INSERT, delete, in which case the return value is the number of rows affected by the command. For other statements, such as set or create, return-1, if a rollback occurs, the return value is also-1.

2, Excutereader method.
It is used with the query command to return an instance of the SqlDataReader class for a data reader object. The method executes the UPDATE statement, and after successful execution, no rows of the affected data are returned.

3, ExecuteScalar method.
If you want to retrieve only the values in the database information, you do not need to return the database information in the form of a table or a data stream. For example, you simply return the result of an aggregate function such as COUNT (*), SUM (price), or AVG (Quantity), which is appropriate for the method. If the method is called in a regular query statement, only the value of the first column of the first row is read, and all other values are discarded

ExecuteReader: Can read a record of a table, can read only can not write

ExecuteScalar: Can only read a record, generally used to determine whether the database has data, etc., can only read can not write

ExecuteNonQuery: Can be written to read,

First of all, we know that these three things should be ADO in the database operation of the CMD, we know, if we want to make a network to build, inevitably in this process, we must do with the database interaction, we have to continue to read and write the database, then ExecuteNonQuery (), ExecuteReader () and ExecuteScalar () are the things we use when working with databases, and I'll take a look at them in turn:

ExecuteNonQuery (): This stuff allows us to change the data in the database without using a dataset while updating the database, using ExecuteNonQuery. By executing an UPDATE, INSERT, or DELETE SQL statement, you can achieve the purpose of changing the data in the database. Even if ExecuteNonQuery does not return any rows, the output parameter that is mapped to the parameter or the return value is still populated with data. However, for UPDATE, INSERT, and DELETE SQL statements, the value returned is the number of rows affected by the ExecuteNonQuery () command. For other types of statements and fallback, the value returned is-1.

ExecuteReader (): We usually use the Recordset object in ASP to read the data from the database, and to use the loop to read the data in a single, but in our ADO, we are using DataReader The ExecuteReader () method of the object to list the data, and we use this ExecuteReader () method to display the data is the fastest way, because when we are using the ExecuteReader () method of DataReader Object for the data to be displayed in the construction of the site, he can only read one forward and not return, that is, like an ASP in the ADO method of the Recordset object MoveNext, he did not move-1 such a return method.

ExecuteScalar (): This kind of cmd is equivalent to the function we use in ADO with some computational functions in SQL statements, where we just return the first column of data for the first row of all affected rows, so that is equivalent to professional for us such as: SELECT COUNT (*) From database function, we can use

String Sql= "SELECT * From Database"

SqlCommand Cmd=sqlcommand (Sql,conn)

int i= (int) cmd. ExecuteScalar ()

So the value of our local change I is the same as my function above.

Finally, it is worth saying: This is from the time I usually use some of the knowledge: that is, if we are in the data operation, if there is no data to operate, then we can only use ExecuteReader () This cmd, and executenoequery () With ExecuteScalar () if it is used without data, an error is "No instantiation of the image". So we can only use the Read () method in ExecuteReader () to detect if there is data.

Ado. ExecuteNonQuery, Excutereader, and ExecuteScalar Methods for command objects in net

Related Article

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: 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.