Getting started with ADO. Net: executenonquery, executescalar, and executereader

Source: Internet
Author: User

Now let's take a look at the three execute methods of the sqlcommand class: executenonquery, executescalar, and executereader.
Executenonquery is used to execute a SQL statement or stored procedure that doesn't
Return any records. You'll use this method when executing operations that update, insert, or
Delete information in the database. executenonquery returns an integer value that specifies how
Explain rows were affected by the query-This proves useful if you want to know, for example,
How many rows were deleted by the last delete operation. Of course, in case you don't need
Know that number, you can simply ignore the return value. Here's a simple piece of code that
Shows how to open the connection, execute the command using executenonquery, and imme-
Diately close the connection afterward:
Connection. open ();
Command. executenonquery ();
Command. Close ();
Executescalar is like executenonquery in that it returns a single value, although it returns
Value that has been read from the database instead of the number of affected rows. It is used in
Conjunction with select statements that select a single value. If select returns more rows and/or
More columns, only the first column in the first row is returned. A typical SQL query that shocould be
Executed using executescalar is select count (*) from department-which returns
Number of rows in the department table.
Executereader is used with select statements that return multiple records (with any
Number of fields). executereader returns a sqldatareader object, which contains the results
The query. A sqldatareader object reads and returns the results one by one, in a forward-only
And read-only manner. The good news about the sqldatareader is that it represents the fastest
Way to read data from the database, and the bad news is that it needs an open connection
Operate-no other database operations can be performed med on that connection until the reader is closed. In our solution, you'll load all the data returned by the sqldatareader into a datatable
Object (which is capable of storing the data offline without needing an open connection ),
Which will allow you to close the database connection very quickly.

The datatable class can store a result set locally without needing an open connection
SQL Server, and it isn' t data provider-specific, like the other ADO. Net objects mentioned so
FAR (whose names begin with SQL because they're SQL Server-specific ).

A "parent" of the datatable object is the dataset, which is a very smart object that represents
Something like an in-memory database. dataset is capable of storing data tables, their data types, relation-
Ships between tables, and so on. Because of their complexity, datasets consume a lot of memory, so it's
Good to avoid them when possible.

 

Not original, copied from ,:Beginning ASP. Net e-commerce in C # from novice to professional

 

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.