Documentation Home

Tip

All examples used are valid against the Microsoft Northwind Sample Database You can run the resulting SQL from the examples against Northwind in order to test and play with the SqlObjects library.

Parameterized QueriesΒΆ

The SqlObjects library supports of the crafting of parameterized queries by specifying variables in the @paramVar format using the @ prefix. When setting values to a string in the @paramVar format then it will be printed in the output without any quotes as shown below, after which the resulting query can be passed to a Dapper method or any other ORM that supports parameterized queries.

var sql = TSQL

    .SELECT()
      .STAR()
    .FROM("Products")
    .WHERE("ProductId").IsEqualTo("@ProductId")
    .Build()

;

Console.WriteLine(sql);

Output:

SELECT
 *
FROM [Products]
WHERE 1=1
 AND [ProductId] = @productId

WHERE 1=1 ?

Then somewhere using Dapper ORM for example:

var paramObj = new { ProductId = 123 };

var product = connection.Query<Product>(sql, paramObj);

You can specify @paramVar variables anywhere in your query where it makes sense and it will render the variable accordingly.