If a .NET application (Web, Desktop, Console etc.) has to retrieve data, then the application needs to
1. Connect to the Database
2. Prepare an SQL Command
3. Execute the Command
4. Retrieve the results and display in the application
This is the normal flow which is used by any .Net application, not only to retrieve data but also to insert, update or Delete data. In our previous article Read here we learnt , how to establish a connection to SQL server database properly and at the same time we also learnt to close the connection properly .
The next step is to execute the command on the server , And SQL Command Class helps us to achieve this requirement. SqlCommand class is used to prepare an SQL statement or StoredProcedure that we want to execute on a SQL Server database. In this article, we will discuss about executing Transact-SQL statements on a SQL Server. In a later article we will learn about executing stored procedures using the SqlCommand class.
SqlCommand Signature
It is a sealed class means it cannot be inherited.
Constructors
This class provides the following constructors.
Constructor | Description |
---|---|
SqlCommand() | It is used to initialize a new instance of the SqlCommand class. |
SqlCommand(String) | It is used to initialize a new instance of the SqlCommand class with T-Sql statement as parameter. |
SqlCommand(String, SqlConnection) | It is used to initialize a new instance of the SqlCommand class. It takes two parameters, first is query string and second is connection string. |
SqlCommand(String, SqlConnection, SqlTransaction) | It is used to initialize a new instance of the SqlCommand class. It takes three parameters query, connection and transaction string respectively. |
SqlCommand(String, SqlConnection, SqlTransaction, SqlCommandColumnEncryptionSetting) | It Initializes a new instance of the SqlCommand class with specified command text, connection, transaction, and encryption setting. |
We can use any of the above version of Sql Command as per our convenient and requirement.
Important Methods of SqlCommand ClassMethod | Description |
---|---|
BeginExecuteNonQuery() | use it to asynchronously execute the command |
CreateParameter() | use it to create a new instance of a SqlParameter object. |
ExecuteReader() | Use it to fetch multiple rows from Sql Server |
ExecuteScalar() | Use it to fetch a single value from server, It executes the query and returns the first column of the first row in the result set, Additional columns or rows are simply ignored. |
Among all specified methods, following are the most commonly used methods of the SqlCommand class.
ExecuteReader - It is used when the T-SQL statement returns more than a single value. For example, if the query returns rows of data like all employee details.
ExecuteNonQuery - it is used when you want to perform an Insert, Update or Delete operation like update or Delete an employee detail or insert employee data.
ExecuteScalar - it is used when the query returns a single(scalar) value. For example, queries that return the total number of rows in a table, or the employee who gets highest salary etc.
Let's do some practical demonstration
We will be using Employee table for our example. Execute the following SQL script using SQL Server Management Studio. The script creates:-
1. Test Database.
2. Employee table under Test Database. and
&emsp
3. Populate it with some data
we know there are multiple constructors available for SqlCommand Class , among all, two of them are widely used to create an instance of SqlCommand Class.
1.The Parameterized constructor which takes two parameter
The first parameter is a string here we specify the command we want to execute on server or the Stored Procedure Name and the second parameter is of connection type where we specify the SqlConnection object.
The sample code above executes a T-SQL statement, that returns multiple rows of data using the ExecuteReader() method. In this example, we are creating an instance of SqlCommand class, in just one line, by passing in the command text, and the connection object. For this purpose, we are using an overloaded constructor of the SqlCommand class that takes 2 parameters first the command Text, and, second the connection.
.2. The parameter less constructor of SqlCommand Class.
Here , we first instantiate the SqlCommand Class using the parameter less constructor then later specify the command text and connection, using the CommandText and Connection properties of the SqlCommand object as shown below.
Example of ExecuteScalar()
Return the count of all Employee in table.In the example below, we are using ExecuteScalar() method, as the T-SQL statement returns a single value.
Example of ExecuteNonQuery()
Insert ,Update and Delete operation on Employee tableThe following examples perform an Insert, Update and Delete operations on a SQL server database using the ExecuteNonQuery() method of the SqlCommand object.