The example above contains two TextBox controls named tbFirstname and tbLastname. When the values are entered into the two Textbox controls and Submit clicked, the Button Click subroutine is executed.
The Button Click subroutine creates an instance of the SqlCommand class with a parametric Select statement. Two instances of the SqlParameter class are created and added to the Parameters collection of the SqlCommand class. For example, the @firstname parameter is created and added with the following statement:
This statement creates a new SqlParameter named @firstname and further adds to the Parameters collection of the SqlCommand class. Passing its name and value creates the parameter. In this case, the value of the parameter is the value of the tbFirstname TextBox control.
Using Parameters with MS Access
When using the classes from the System.Data.OleDb namespace, you need to create the parameters for MS Access. For example, to retrieve a phone number for an employee from the Employees table of Northwind database, you have to write the SQL statement like: -
What is Character?
This represents the parameter instead of using a named parameter like @firstname or @lastname.
Important Note: - (i) Use @ character for SQL SERVER
(ii) Use? Or @ character with MS Access.
But is? Recommended with MS ACCESS
Example 24 illustrates how you can use parametric queries with a Microsoft Access database.
The output of above example is shown below: