In this Video, I am going to discuss ADO.NET Using Stored Procedure in detail. Please read our previous Video, where we discussed ADO.NET DataSet Architecture. At the end of this Video, you will understand how to call a stored procedure without parameter as well as how to call a stored procedure from ADO.NET using both inputs as well as the output parameter.

Before understanding how to call a stored procedure, let us first understand what is a stored procedure.
What is a Stored Procedure in SQL?
A Stored Procedure in SQL is a database object which contains pre-compiled SQL Statements. In simple words, we can also say that the Stored Procedures are a block of code that is designed to perform a specific task whenever it is called. Please click here to learn SQL Server Stored Procedure in detail.
Example to understand ADO.NET using Stored Procedure:
We are going to use the following student table in this demo to understand the concept of ADO.NET using Stored Procedure.
Please use the below SQL Script to create and populate the database StudentDB and table Student with the required sample data.
So, the first thing is always first. Let us create a stored procedure that will not take any input parameter but will return all the records from the student table. Please use the below SQL Script to create the stored procedure.
How to call a stored procedure using C# ADO.NET.
Let us see the step-by-step procedure to call the above-stored procedure. Please have a look at the below image. So, what you need to do is, first create an instance of command object and then specify the commandTest property value as the stored procedure name and the most important point is you need to specify the command type as Stored Procedure.
You can also use the other overloaded constructor of the Command class as shown in the below image. As you can see, here we are passing the stored procedure name and the connection object to the constructor of the command object and then specify the command type as a Stored procedure.
The complete example is given below.
Output:
Stored procedure with Input Parameter:
Now, we will see how to call a stored procedure with an input parameter. So, please use the below SQL Script to create the stored procedure which will return the student details by id. Here, student id is the input parameter and that parameter value we need to pass while calling this stored procedure.
How to call a stored procedure with an input parameter in C# ADO.NET?
In order to understand how to call a stored procedure with an input parameter, please have a look at the following image. We already discussed the command object. The point that you need to focus on here is the SqlParameter object. As you can see here we are creating an instance of the SqlParameter object and then setting the parameter name, the data type, value, and the direction of the parameter.
The complete example is given below.
Output:
Stored Procedure with both Input and Output Parameter:
In our previous example, we understand how to call a stored procedure with an input parameter. Now, let us see how to call a stored procedure with both input and output parameters. So, please use the below SQL Script to create the stored procedure with both input and output parameters.
As you can see the above-stored procedure, takes four parameters (3 input + 1 output). The above-stored procedure is very simple, takes the Student Name, Email and Mobile and inserts it into the Student table. As we created the student table with Id as an Identity column, so, we dont require to pass the id value in the insert statement. But what we want here is we need to return the newly created student id and this is where the output parameter comes into the picture. Here, we set the output parameter value with the newly generated student id.
How to call a stored procedure with both input and output parameters in C#?
In order to understand how to call a stored procedure with both input and output parameters, please have a look at the following image. As you can see, while defining the Output Parameter you need to specify the parameter direction as Output and you dont require to set the value property.
Note: By default the parameter direction is Input.
The complete code is given below.
Output: