Show / Hide Table of Contents

    ISqlConditionBuilder.ToSqlString(DataContext context) Method

    .NET Standard 2.x

    Returns the raw SQL string for the current search condition in the corresponding database.

    Namespace: SnapObjects.Data

    Assembly: SnapObjects.Data.dll

    Syntax

     string ToSqlString(DataContext context);
    

    Parameters

    context SnapObjects.Data.DataContext

    A DataContext object which contains the database connection information.

    Returns

    System.String

    Returns the raw SQL string.

    Examples

    The following code example demonstrates how to use the ToSqlString method. It converts the search condition to the raw SQL for the database corresponding to the data context.

    using SnapObjects.Data;
    using System;
    
    namespace Appeon.ApiDoc.ISqlConditionBuilderExamples
    {
        public class ToSqlStringExample
        {
            private SchoolContext _context;
    
            public ToSqlStringExample(SchoolContext dataContext)
            {
                // Sets the Data Context
                _context = dataContext;
            }
    
            public void Example()
            {
                // Gets the search condition in the WHERE clause.
                var sqlWhereCondition = SqlBuilder.WhereValue("DepartmentId", 1);
    
                // Converts the search condition to raw SQL for the database corresponding to data context.
                Console.WriteLine("The search condition is: \n{0}",
                    sqlWhereCondition.ToSqlString(_context));
    
                // Defines a SQL statement with the search condition.
                var sqlbuilder = SqlBuilder.Select("name")
                                            .From("Department")
                                            .Where(sqlWhereCondition);
    
                // Converts the syntax to raw SQL for the database corresponding to data context.
                string sql = sqlbuilder.ToSqlString(_context);
    
                Console.WriteLine();
                Console.WriteLine("The SQL SELECT statement: \n{0}", sql);
    
                /*This code produces the following output:
                
                The search condition is:
                WHERE ([DepartmentId] = 1)
    
                The SQL SELECT statement:
                SELECT
                 [name]
                FROM [Department]
                WHERE ([DepartmentId] = 1)           
                */
            }
        }
    }
    

    Applies to

    .NET Standard

    2.x

    Back to top Generated by Appeon