ASP.NET 101 Home » Tutorials » The 3 'Execute' Command Methods in ADO.NET
The 3 'Execute' Command Methods in ADO.NET
04. Jun, 2007 by admin
Here, we’re going to look at the most often used or needed methods of the command object (OleDbCommand or SQLCommand), their differences and how each one is used. They are the ExecuteReader, ExecuteNonQuery, and ExecuteScalar methods. Once you make a connection to your database and create a command, you’re going to need one of these methods to execute the command.
Simple Overview:
- ExecuteReader – basically, this method returns a DataReader which is filled with the data that is retrieved using the command.
This is known as a forward-only retrieval of records – it uses your sql statement to read through the table from the first to the last.
There are many DataReader examples on this site. Just go to http://aspnet101.com/aspnet101/aspnetcode.aspx and choose DataReader from the DropDownList
Useage: cmd.ExecuteReader
- ExecuteNonQuery – this method returns no data at all. It is used mainly with Inserts and Updates of tables.
Here, also, we have many code samples using ExecuteNonQuery.
Inserting records
Updating a Record
Useage: cmd.ExecuteNonQuery
- ExecuteScalar – Returns only one value after executing the query – it returns the first field in the first row. This is very light-weight and is perfect when all your query asks for is one item.
This would be excellent for receiving a count of records (Select Count(*)) in an sql statement, or for any query where only one specific field in one column is needed.
Useage: cmd.ExecuteScalar
|
Related Posts:
No comments yet... Be the first to leave a reply!