SQL commands in ADO. NET operate databases

Source: Internet
Author: User

We need to constantly read and write the database, so ExecuteNonQuery (), ExecuteReader () and ExecuteScalar () are what we need to use in database operations. Let's take a look at them one by one:
ExecuteNonQuery (): This item allows us to use ExecuteNonQuery to change data in the database without using DataSet when updating the database. You can modify data in the database by executing UPDATE, INSERT, or delete SQL statements. Even if ExecuteNonQuery does not return any rows, the output parameters or return values mapped to the parameters are still filled with data. However, for UPDATE, INSERT, and delete SQL statements, the returned value is the number of rows affected by the ExecuteNonQuery () command. For other types of statements and rollback, the returned value is-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 of the DataReader object 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 display in website construction, it can read forward only one by one and cannot return, that is, like Movenext of the Recordset object in the ADO method in ASP, there is no return method like move-1.
ExecuteScalar (): This CMD is equivalent to using some computing functions in SQL statements in ADO. Here we only return the first column of data in the first row of all affected rows, in this way, it is equivalent to the function of selecting 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 do not have data to operate, we can only use ExecuteReader () executeNoeQuery () and Executescalar (), if they are used when no data is available, the "no Object Instantiation" error will occur. Therefore, we can only use the REad () method in ExecuteReader () to check whether data exists.

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.