Microsoft Docs

Commands and Parameters in ADO.NET

ADO.NET provides a rich set of classes for interacting with data sources, and at the core of data manipulation are Command objects. These objects are used to execute SQL statements and stored procedures against a database.

Understanding Command Objects

A Command object represents a SQL statement or stored procedure that you want to execute. The specific type of Command object depends on the data provider you are using (e.g., SqlCommand for SQL Server, OracleCommand for Oracle, MySql.Data.MySqlClient.MySqlCommand for MySQL).

Key properties of a Command object include:

Working with Parameters

Parameters are essential for several reasons:

The Parameters collection of a Command object is used to add, remove, and manage parameters. Each parameter is represented by a Parameter object (e.g., SqlParameter, OracleParameter).

When adding a parameter, you typically specify:

Example: Executing a SELECT statement with parameters

This example demonstrates how to use parameters with a SqlCommand to retrieve data based on a user ID.


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

public class CustomerDataAccess
{
    private string connectionString = "YourConnectionStringHere";

    public string GetCustomerName(int customerId)
    {
        string customerName = null;
        using (SqlConnection connection = new SqlConnection(connectionString))
        {
            string query = "SELECT CustomerName FROM Customers WHERE CustomerID = @CustomerID";

            using (SqlCommand command = new SqlCommand(query, connection))
            {
                // Add the parameter
                SqlParameter customerIdParam = new SqlParameter("@CustomerID", SqlDbType.Int);
                customerIdParam.Value = customerId;
                command.Parameters.Add(customerIdParam);

                connection.Open();
                object result = command.ExecuteScalar(); // ExecuteScalar returns the first column of the first row

                if (result != null)
                {
                    customerName = result.ToString();
                }
            }
        }
        return customerName;
    }
}
                

Parameter Naming Conventions

Different database providers use different conventions for parameter names. It's crucial to match these conventions:

It's generally recommended to use named parameters as they are more readable and less prone to ordering errors, especially when dealing with multiple parameters.

Output Parameters and Return Values

Stored procedures can also use output parameters and return values to pass information back to the application. These are configured using the Direction property of the Parameter object.

Example: Using an Output Parameter

This example assumes a stored procedure that takes a CustomerID and returns the corresponding CustomerName via an output parameter.


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

public class StoredProcedureExample
{
    private string connectionString = "YourConnectionStringHere";

    public string GetCustomerNameFromSP(int customerId)
    {
        string customerName = null;
        using (SqlConnection connection = new SqlConnection(connectionString))
        {
            using (SqlCommand command = new SqlCommand("GetCustomerNameSP", connection))
            {
                command.CommandType = CommandType.StoredProcedure;

                // Input parameter
                command.Parameters.AddWithValue("@CustomerID", customerId);

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

                connection.Open();
                command.ExecuteNonQuery(); // ExecuteNonQuery for stored procedures that don't return a result set

                customerName = outputParam.Value.ToString();
            }
        }
        return customerName;
    }
}
                

Best Practices