Databases Reference
In-Depth Information
n
Note
Some data providers use the standard SQL
parameter marker
, a question mark (
?
), instead of
named parameters.
Command parameters have several advantages:
• The mapping between the variables and where they're used in SQL is clearer.
• Parameters let you use the type definitions that are specific to a particular
ADO.NET data provider to ensure that your variables are mapped to the correct
SQL data types.
• Parameters let you use the
Prepare
method, which can make your code run faster
because the SQL in a “prepared” command is parsed by SQL Server only the first
time it's executed. Subsequent executions run the same SQL, changing only
parameter values.
• Parameters are used extensively in other programming techniques, such as using
stored procedures and working with irregular data.
Try It Out: Using Command Parameters
To try out using command parameters, follow these steps:
1.
Add a new C# Console Application project named CommandParameters to your
Chapter11 solution. Rename
Program.cs
to
CommandParameters.cs
.
2.
Replace the code in
CommandParameters.cs
with the code in Listing 11-5. This is a
variation of Listing 11-4, with salient changes highlighted in bold.
Listing 11-5.
CommandParameters.cs
using System;
using System.Data;
using System.Data.SqlClient;
namespace Chapter11
{
class CommandParameters
{
static void Main()
Search WWH ::
Custom Search