Search code examples
ado.netdbextensions

Multiple queries with DBExtensions SqlBuilder


When using the SqlBuilder class of DBExtensions, is it possible to build multiple select statements that are executed in a single round trip?

Something along the lines of:

select t1.* from Table1 t1 where t1.Foo = 'Bar 1';
select t2.* from Table2 t2 where t2.Foo = 'Bar 2';

Solution

  • For the building part, you can do:

    var query1 = SQL
       .SELECT("t1.*")
       .FROM("Table1 t1")
       .WHERE("t1.Foo = {0}", "Bar 1");
    
    var query2 = SQL
       .SELECT("t2.*")
       .FROM("Table2 t2")
       .WHERE("t2.Foo = {0}", "Bar 2");
    
    var batchQuery = SqlBuilder.JoinSql(";", query1, query2);
    

    About execution, I have no idea if your ADO.NET provider supports batch SELECT queries, maybe you want to do a UNION query instead?