#Description: Example code demonstrating creating a SELECT query builder and opening a DataReader.
#Remarks: Use a DataReader for forward-only access. Note, this has both performance (overall speed and time before first row is available) and memory advantages over a DataTable.
#Author: Intersoft Systems Limited
#Version: 1.03
#Last Updated: 07/02/2024

Dim dr As IDataReader
Dim sqb As ISSelectQueryBuilder

' Create Select Query Builder
sqb = finBL.Database.CreateSelectQueryBuilder()

With sqb
  ' Clear
  .Clear()

  ' Set Table and other Options
  .Table = "Account"
  '.DistinctType = iseSelectQueryDistinctType.Distinct
  .TopLimit = 10

  ' Select Fields
  With .Fields
    .Add("Account.Pk", "AccountPk")
    .AddList("Account.AccountId,Account.Name")
    .AddList("Client.ClientId")
  End With

  ' Joins
  With .Joins
    .Add("Client", "Client.Pk", "Account.ClientPk", iseSelectQueryJoinType.Inner)
  End With

  ' Where
  With .SqlWhere
    ' Only Open Accounts
    .AppendComparisonInteger("Account.Status", "=", CInt(isefinAccountStatus.Open))

    ' User Filters - ALWAYS INCLUDE
    .Append(finBL.CurrentUserInformation.FilterAccountSqlWhere)
  End With

  ' Order By
  With .OrderByFields
    .Add("Account.AccountId")
  End With

  ' Open DataReader
	Main = .ExecuteDisconnectedDataReader(dr)
End With  
	
If Main Then
  ' Read each Row
  Do While dr.Read
    finBL.DebugPrintFormat("Account {0}, '{1}' belongs to client '{2}'", finBL.Database.GetFieldString(dr!AccountId), finBL.Database.GetFieldString(dr!Name), finBL.Database.GetFieldString(dr!ClientId))
  Loop

  ' Close the DataReader 
  finBL.Database.DataReaderClose(dr)
End If