MSDN Documentation

Microsoft Developer Network

Using Parameters with ADO.NET

Parameters are a crucial feature in ADO.NET for building secure and efficient data access applications. They help prevent SQL injection attacks by separating SQL code from user-supplied data, and can also improve performance by allowing the database to cache query plans.

Why Use Parameters?

Creating and Using Parameters

The process of using parameters generally involves the following steps:

  1. Create a command object (e.g., SqlCommand, OleDbCommand).
  2. Define the SQL statement with placeholders for parameters (e.g., @paramName, ?).
  3. Create a parameter object (e.g., SqlParameter, OleDbParameter).
  4. Set the parameter's name, data type, size (if applicable), and value.
  5. Add the parameter object to the command's Parameters collection.
  6. Execute the command.

Example: Using Parameters with SqlCommand

This example demonstrates how to use parameters with SQL Server.


using System;
using System.Data;
using System.Data.SqlClient;

public class ParameterExample
{
    public static void AddNewProduct(string productName, decimal price)
    {
        string connectionString = "Your_Connection_String_Here"; // Replace with your actual connection string

        using (SqlConnection connection = new SqlConnection(connectionString))
        {
            string query = "INSERT INTO Products (ProductName, UnitPrice) VALUES (@Name, @Price)";

            using (SqlCommand command = new SqlCommand(query, connection))
            {
                // Add parameters
                command.Parameters.AddWithValue("@Name", productName);
                command.Parameters.AddWithValue("@Price", price);

                // Alternative: Explicitly define parameter types
                // SqlParameter nameParam = new SqlParameter("@Name", SqlDbType.NVarChar, 50);
                // nameParam.Value = productName;
                // command.Parameters.Add(nameParam);

                // SqlParameter priceParam = new SqlParameter("@Price", SqlDbType.Decimal);
                // priceParam.Value = price;
                // command.Parameters.Add(priceParam);

                try
                {
                    connection.Open();
                    int rowsAffected = command.ExecuteNonQuery();
                    Console.WriteLine($"{rowsAffected} row(s) inserted.");
                }
                catch (SqlException ex)
                {
                    Console.WriteLine($"Error: {ex.Message}");
                }
            }
        }
    }

    public static void Main(string[] args)
    {
        AddNewProduct("Chai", 18.00m);
        AddNewProduct("Chang", 19.00m);
    }
}

Parameter Syntax Differences

The syntax for parameter placeholders can vary depending on the data provider:

Data Provider Parameter Placeholder Syntax
SQL Server (SqlClient) @ParameterName
OLE DB (OleDb) ?
ODBC (Odbc) ?
Oracle (OracleClient) :ParameterName

AddWithValue vs. Explicit Parameter Creation

The AddWithValue method is a convenient shortcut for creating and adding parameters. However, it can sometimes lead to performance issues because ADO.NET has to infer the data type, which might not always be optimal. For better control and performance, it's often recommended to explicitly create and configure each parameter.

Note: When using AddWithValue, ensure the .NET Framework data type you pass is compatible with the database column's data type.

Explicit Parameter Creation Example


using System.Data;
using System.Data.SqlClient;

// ... inside a method ...

SqlCommand command = new SqlCommand("UPDATE Employees SET Salary = @NewSalary WHERE EmployeeID = @ID", connection);

SqlParameter salaryParam = new SqlParameter("@NewSalary", SqlDbType.Money);
salaryParam.Value = 55000.00m;
command.Parameters.Add(salaryParam);

SqlParameter idParam = new SqlParameter("@ID", SqlDbType.Int);
idParam.Value = 101;
command.Parameters.Add(idParam);

Output Parameters

Parameters can also be used to retrieve values from stored procedures. These are called output parameters.


using System;
using System.Data;
using System.Data.SqlClient;

public class OutputParameterExample
{
    public static int GetProductCount(string categoryName)
    {
        string connectionString = "Your_Connection_String_Here";
        int count = 0;

        using (SqlConnection connection = new SqlConnection(connectionString))
        {
            using (SqlCommand command = new SqlCommand("GetProductCountByCategory", connection))
            {
                command.CommandType = CommandType.StoredProcedure;

                // Input parameter
                command.Parameters.AddWithValue("@CategoryName", categoryName);

                // Output parameter
                SqlParameter outputParam = new SqlParameter("@ProductCount", SqlDbType.Int);
                outputParam.Direction = ParameterDirection.Output;
                command.Parameters.Add(outputParam);

                try
                {
                    connection.Open();
                    command.ExecuteNonQuery();
                    count = (int)outputParam.Value;
                }
                catch (SqlException ex)
                {
                    Console.WriteLine($"Error: {ex.Message}");
                }
            }
        }
        return count;
    }

    // Assume a stored procedure like this exists in SQL Server:
    // CREATE PROCEDURE GetProductCountByCategory
    //     @CategoryName NVARCHAR(50),
    //     @ProductCount INT OUTPUT
    // AS
    // BEGIN
    //     SELECT @ProductCount = COUNT(*) FROM Products WHERE Category = @CategoryName;
    // END
}

Parameter Direction

Parameters can have different directions:

Important: Always use parameters when executing SQL queries that involve user input. This is fundamental for secure application development.