Show / Hide Table of Contents

    ISqlBuilder.AddParameters(params ISqlParameter[] parameters) Method

    .NET Standard 2.x

    Adds the parameters which will be used in the SQL statement to the current object.

    Namespace: SnapObjects.Data

    Assembly: SnapObjects.Data.dll

    Syntax

     bool AddParameters(params ISqlParameter[] parameters);
    

    Parameters

    parameters SnapObjects.Data.ISqlParameter[]

    One or more SQL parameters to be used in the SQL statement.

    You can use the SqlBuilder.Parameter method to create a SQL parameter.

    Returns

    System.Boolean

    Returns false if the parameter name is not specified or the parameter name already exists; otherwise, returns true.

    Remarks

    When building with a raw SQL, you can use parameter placeholders in the raw SQL statement. It is recommended to use the syntax for parameter placeholders that is specific to the data source, for example, uses @parametername for SQL Server and uses :parametername for Oracle.

    Examples

    The following code example demonstrates how to use the AddParameters method to add two parameters to a SqlQueryBuilder object.

    using SnapObjects.Data;
    using System;
    
    namespace Appeon.ApiDoc.ISqlBuilderExamples
    {
        public class AddParametersExample
        {
            private SchoolContext _context;
    
            public AddParametersExample(SchoolContext dataContext)
            {
                // Sets Data Context.
                _context = dataContext;
            }
    
            public void Example()
            {
                // Builds a SQL statement.
                var sqlBuilder = new SqlQueryBuilder();
    
                // Adds two parameters.
                sqlBuilder.AddParameters(
                    SqlBuilder.Parameter<int>("deptId"),
                    SqlBuilder.Parameter<string>("deptName"));
    
                sqlBuilder.Select("name")
                        .From("Department")
                        .Where("DepartmentId", "@deptid")
                        .OrWhere("name", "@deptName");
    
                Console.WriteLine("The SQL SELECT statement: \n{0}",
                    sqlBuilder.ToSqlString(_context));
    
                // Gets the first "name" column from the result set.
                var scalar =
                    _context.SqlExecutor.Scalar<string>(sqlBuilder, 4, "Economics");
    
                Console.WriteLine();
                Console.WriteLine("The department name: {0}", scalar);
    
                /*This code produces the following output:
                
                The SQL SELECT statement:
                SELECT
                 [name]
                FROM [Department]
                WHERE ([DepartmentId] = @deptId
                OR [name] = @deptName)
    
                The department name: Economics        
                */
            }
        }
    }
    

    Applies to

    .NET Standard

    2.x

    Back to top Generated by Appeon