I have about 500K rows I need to return from my database (please don't ask why).
I will then need to save these results as XML (more URGH) and the ftp this file to somewhere magical.
I also need to transform the each row in the result set.
Right now, this is what I'm doing with say .. TOP 100
results:
Query<T>
method, which throws the entire result set into memory This works fine for 100 rows, but I get an Out Of Memory exception with AutoMapper when trying to convert the 500K results to a new collection.
So, I was wondering if I could do this...
I'm trying to stop throwing everything into RAM. My thinking is that if I can stream stuff, it's more memory efficient as I only work on a single result set of data.
Dapper has great performance because it doesn't translate queries that we write in . NET to SQL. It is important to know that Dapper is SQL Injection safe because we can use parameterized queries, and that's something we should always do. One more important thing is that Dapper supports multiple database providers.
Dapper is a NuGet library that can be added to any project. It extends the IDbConnection interface. The IDbConnection interface represents an open connection to data source implemented by the . NET framework. Every database provider extends this interface to for their database i.e. SQL Server, Oracle, MySQL etc.
That means that the command / reader etc has completed before it returns. As a side note, buffered mode also avoids the oh-so-common "there is already an open reader on the connection" (or whatever the exact phrasing is).
Allow Dapper to manage it: Dapper automatically opens the connection (if it was not opened) and closes it (if it was opened by Dapper) for you. This is similar to DataAdapter. Fill() method.
using Dapper's
Query<T>
method, which throws the entire result set into memory
It is a good job, then, that one of the optional parameters is a bool
that lets you choose whether to buffer or not ;p
Just add , buffer: false
to your existing call to Query<T>
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With