Table of Contents

SqlInOperator<T> Class

Definition

Namespace
Cuemon.Data.SqlClient
Assemblies
Cuemon.Data.SqlClient.dll
Source
src/Cuemon.Data.SqlClient/SqlInOperator.cs

Provides a safe way to include a Transact-SQL WHERE clause with an IN operator to execute against a SQL Server database.

public class SqlInOperator<T> : InOperator<T>

Type Parameters

T

The type of the data in the IN operation of the WHERE clause to execute against a SQL Server database.

Inheritance
object
SqlInOperator<T>
Inherited Members

Examples

The following example demonstrates how to use to safely generate parameterized SQL IN clauses that are protected against SQL injection.

using System;
using System.Data;
using Cuemon.Data;
using Cuemon.Data.SqlClient;
using Microsoft.Data.SqlClient;

namespace MyApp.Examples
{
    public sealed class SqlInOperatorExample
    {
        public void Demonstrate()
        {
            var inOperator = new SqlInOperator<string>(() => "@color");
            InOperatorResult result = inOperator.ToSafeResult("Red", "Green", "Blue");

            var commandText = $"SELECT * FROM Products WHERE Color IN ({result})";
            using var command = new SqlCommand(commandText);

            foreach (IDataParameter dbParameter in result.ToParametersArray())
            {
                command.Parameters.Add((SqlParameter)dbParameter);
                Console.WriteLine($"{dbParameter.ParameterName} = {dbParameter.Value}");
            }

            Console.WriteLine(command.CommandText);
            Console.WriteLine(string.Join(", ", result.Arguments));
        }
    }
}

Constructors

Name Description
SqlInOperator(Func<string>)

Initializes a new instance of the InOperator<T> class.

Methods

Name Description
ParametersSelector(T, int)

A callback method that is responsible for the values passed to the ToSafeResult(params T[]) method.