Show / Hide Table of Contents

    ISqlBuilderBase.Validate(DataContext context, bool throwError = true) Method

    .NET Standard 2.x

    Validates the raw SQL string for the specified database generated by the current sqlbuilder.

    Namespace: SnapObjects.Data

    Assembly: SnapObjects.Data.dll

    Syntax

      bool Validate(DataContext context, bool throwError = true);
    

    Parameters

    context SnapObjects.Data.DataContext

    A DataContext object which contains the database connection information.

    throwError System.Boolean

    Optional.

    True (default ) -- to throw an exception if the raw SQL is not valid in the specified database;

    False -- Not to throw an exception if the raw SQL is not valid in the specified database.

    Returns

    System.Boolean

    Returns true if the raw SQL is valid in the specified database; false if the raw SQL is not valid in the specified database.

    Examples

    The following code example demonstrates how to validate a SqlQueryBuilder object.

    using SnapObjects.Data;
    using System;
    
    namespace Appeon.ApiDoc.ISqlBuilderBaseExamples
    {
        public class ValidateExample
        {
            private SchoolContext _context;
    
            public ValidateExample(SchoolContext dataContext)
            {
                // Sets Data Context
                _context = dataContext;
            }
    
            public void Example()
            {
                // Builds a SQL statement.
                var sqlBuilder = new SqlQueryBuilder();
                sqlBuilder.Select("name")
                        .From("Department")
                        .WhereValue("DepartmentId", 1);
    
    
                // Validates the SQL statement in database. 
                bool valid = sqlBuilder.Validate(_context);
                Console.WriteLine("Validation Result (first time): {0}", valid);
                Console.WriteLine(sqlBuilder.ToSqlString(_context));
    
                // Adds a wrong search condition in the WHERE clause.
                sqlBuilder.RemoveWhere();
                sqlBuilder.Where("CourseId", "1");
    
                // Validates without throwing the exception.
                valid = sqlBuilder.Validate(_context, throwError: false);
                Console.WriteLine();
                Console.WriteLine("Validation Result (second time): {0}", valid);
                Console.WriteLine(sqlBuilder.ToSqlString(_context));
    
                try
                {
                    // Validates and throws the exception.
                    valid = sqlBuilder.Validate(_context, throwError: true);
                }
                catch (Exception e)
                {
                    Console.WriteLine();
                    Console.WriteLine("Exception Message: {0}", e.Message);
                }
    
                /*This code produces the following output:
                
                Validation Result (first time): True
                SELECT
                 [name]
                FROM [Department]
                WHERE ([DepartmentId] = 1)
    
                Validation Result (second time): False
                SELECT
                 [name]
                FROM [Department]
                WHERE ([CourseId] = 1)
    
                Exception Message: Invalid column name 'CourseId'.
                */
            }
        }
    }
    

    Applies to

    .NET Standard

    2.x

    Back to top Generated by Appeon