latest Post

What is SqlCommand Object?

What is SqlCommand Object?

The SqlCommand carries the SQL statement that needs to be executed on the database. SqlCommand carries the command in the CommandText property and this property will be used when the SqlCommand calls any of its execute methods.

The Command Object uses the connection object to execute SQL queries.
The queries can be in the form of Inline text, Stored Procedures or direct Table access.
An important feature of Command object is that it can be used to execute queries and Stored Procedures with Parameters.
If a select query is issued, the result set it returns is usually stored in either a DataSet or a DataReader object.
The three important methods exposed by the SqlCommand object is shown below:

ExecuteScalar
ExecuteNonQuery
ExecuteReader

ExecuteScalar is useful for returning a single value from the database. For example, using this method we can retrieve a sum of sales made by a specific product, total number of records in the employee table, unique id by supplying filtering conditions and so on. Since this method performs faster we do not need to go for the Reader method just to retrieve a single scalar value.

ExecuteNonQuery is useful for performing data manipulation on the database. Simply, the ExecuteNonQuery is for executing the DML statements. The return value of the ExecuteNonQuery is an integral value that represents the number of rows affected by the Operation.

ExecuteReader is used when we need to retrieve rows and columns of data using the SQL select statements. As the data retrieved is a table of data, ExecuteReader returns SqlDataReader. We should iterate through this object to get the required values.

About Mallikarjun A

Mallikarjun A
Recommended Posts × +

0 comments:

Post a Comment