I tried to run a query asynchronously in .NET so that I can read the first few rows while the others are still being transmitted, but I had no luck so far.
The reason I want to to this is that we often need to fetch large tables from the database and while it is possible to not freeze the UI completely with asnyc variants of ExecuteReader()
it doesn't seem to be possible to fetch data row by row so that the user can see the progress and maybe even work with the first portion of data.
What I tried is the following query:
SELECT 'Hello '
WAITFOR DELAY '0:0:10'
SELECT 'World!'
When I run this query with SqlCommand.BeginExecuteReader(callback)
the callback function is called after approximately 10 seconds so it obviously waited for the whole query to finish. I also tried SqlCommand.ExecuteReaderAsync()
with the same results.
My question is the following: Is this even possible in .NET? Or doesn't it work because of my query and would work better if tested with a real query (i.e. large table)?