I am using Dapper with ClickHouse database (https://clickhouse.yandex/). This is distributed column-oriented database. It works well, but query result can be divided into many blocks, so I should use NextResult to retrieve all data. Sample code:
public static void ExecuteSQL(ClickHouseConnection connection, string sql)
{
using (var reader = connection.CreateCommand(sql).ExecuteReader())
{
do
{
while (reader.Read())
{
...
}
}
while (reader.NextResult());
}
}
I'm trying to use Dapper. In order to call NextResult I should use QueryMultiple
method. I made the code:
public static void ExecuteDapperQuery<T>(ClickHouseConnection connection, string sql)
{
Dapper.DefaultTypeMap.MatchNamesWithUnderscores = true;
using (var dapperQuery = connection.QueryMultiple(sql))
{
do
{
var list = dapperQuery.Read<T>().ToList();
... /* Do something with list */
}
while (true);
}
}
but this code throws exception ObjectDisposedException
when all data are retrieved and SQL reader becomes null.
I need to know is there the way to know when I should finish iteration? Or is there any other way to work with that database?
It is because of the infinite do/while loop. You can only call read for the number of statements executed.
I need to know is there the way to know when I should finish iteration?
Check to make sure the reader has not already been consumed before reading the set
using (var gridReader = connection.QueryMultiple(sql)) {
while(!gridReader.IsConsumed) { //<-- query multiple until consumed
var list = gridReader.Read<T>().ToList();
... /* Do something with list */
}
}