SqlBuilder Updates

We have covered the SqlBuilder before, however it has been updated with enhancements in MicroLite 2.0 and 2.1.

MicroLite 2.0 introduced support for calling SQL functions (Average, Count, Min, Max, Sum) and to align Select, the shortcut SelectFrom method was changed.

// Instead of calling
// You should now call
var sqlQuery = SqlBuilder
     .Where("CustomerId = @p0", 1022)

var total = session.Advanced.ExecuteScalar<decimal>(query);

You can also alias the calculated column and use it in the WHERE clause:

var sqlQuery = SqlBuilder
     .Average("Total", columnAlias: "AverageTotal")
     .Where("CustomerId = @p0", 1022)
     .AndWhere("AverageTotal > @p0", 1000.00M)

Group By is also supported:

var sqlQuery = SqlBuilder

It was decided not to support the TOP keyword in SqlBuilder as the Paged method on ISession is specifically designed for paging.

MicroLite 2.1 Enhancements details examples of the further enhancements added to SqlBuilder.

This article was written for version 2.1.0 of the MicroLite framework.


2 thoughts on “SqlBuilder Updates

  1. dcsteele

    Useful additions !
    Is the ‘columnAlias:’ usage part of MicroLite or SQL ?
    What is the significance of the ‘M’ in AndWhere(‘AverageTotal > @p0’, 1000.00M) ?
    Would it be possible to change the & … ; to their normal forms, please ?

    1. trevorpilley Post author

      ‘columnAlias:’ is just the parameter name in MicroLite, in the example it would result in ‘SELECT AVG(Total) AS AverageTotal FROM Invoices WHERE (CustomerId = @p0) AND (AverageTotal > @p1)’.

      The M in 1000.00M is how you define a decimal in C#.


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s