Differences between executereader and executescalar

Source: Internet
Author: User

ExecuteReader: Records of one table can be read but not written.

ExecuteScalar: Only one record can be read. It is generally used to determine whether a database has data. It can only be read but not written.

ExecuteNonQuery: can be written to read,

First, we know that these three things should be the CMD for database operations in ADO. NET. We know that if we
To build a network, we must interact with the database in this process.
ExecuteNonQuery (), ExecuteReader () and ExecuteScalar () are our
This is required for database operations. Let's take a look at it in sequence:

ExecuteNonQuery (): This Stuff allows us to use it when updating the database without using DataSet.
ExecuteNonQuery changes data in the database. By executing the UPDATE, INSERT, or delete SQL statement
To change the data in the database. Even if ExecuteNonQuery does not return any rows
The output parameter or return value is still filled with data. However, for UPDATE, INSERT, and delete SQL statements
The value is the number of rows affected by the ExecuteNonQuery () command. Return Value for other types of statements and rollback
All are-1.

ExecuteReader (): we usually use the Recordset object in asp to read data from the database and use
Loop statements to read data one by one, but in our ADO. NET, we use
The ExecuteReader () method is used to list data, and we use this ExecuteReader () method to display data.
Is the fastest way, because when we use the DataReader object in ExecuteReader () method for data
In the construction of the website, he can only read forward one by one and cannot return, that is, like the ADO method in ASP
The same as the Movenext of the Recordset object in, it does not have a return method like move-1.

ExecuteScalar (): This CMD is equivalent to using some computing functions in SQL statements in ADO.
Yes. Here we only return the first column of data in the first row of all affected rows, which is equivalent
For example, select count (*) from database, we can use

String SQL = "select * from database"

SqlCommand cmd = SqlCommand (SQL, conn)

Int I = (int) cmd. ExecuteScalar ()

In this way, our local change I value is the same as my function above.

Finally, it is worth mentioning that this is from some of my usual experiences: If we enter the number of rows
If no data is operable during data operations, we can only use the CMD ExecuteReader ().
If executeNoeQuery () and Executescalar () are used when no data is available, an error occurs.
Instantiation error. Therefore, when determining whether data exists, we can only use the REad () method in ExecuteReader ()
Method to detect.

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