The output is shown below:
Queries Using Parameters
Queries using parameters are performed to make ADO.NET more efficient. A parameter is a value that’s either passed in or out of a query. Using parameters helps you to keep your information straight and makes your queries comprehensible. For example you need to retrieve all employees from a table who works in department no. 10.In this case the SQL query will be: -
This will give a fixed result and the scope will be limited to department number 10. If we want that each record is changed by passing outside parameters the query will be as: -
This will serve the purpose of building a dynamic query, but an unrecognized one. What if you have three different textboxes?
Having them would become difficult for a developer, who is trying to read the code to know as to which box contains which information.
How ever, a more efficient method is to use parameters.
Let us replace previous query with a parametric one: -
here @deptNo represents the parameter.
Using Parameters with Microsoft SQL Server
When you execute a SQL statement with the SqlCommand class, parameters are represented in the statement like:
In this statement, @firstname and @lastname represent parameters.