How to serialize and deserialize results of SQL queries in generic way


Staff member
I am not satisfied with answers on <a href="">Caching SQL queries</a>.

I am not interested in 3rd party solutions because this should be a simple problem to solve by myself.

I am looking for an alternative solution to <a href="" rel="nofollow noreferrer">Caching of Linq queries</a> which works with any SQL query, not just Linq. Ideal solution would be to have stored procedure like execsql @Sql,@HashCode, which would query table of serialized results by HashCode and return result if exists, otherwise run query, save serialized result in table of results and return it. Of course returned results must have the same structure as if they were not stored.

Let's not worry about cache expiration/garbage cleaning or other possible performance issues in this context.

In order to do that I need to be able to detect hash code of SQL query (I suppose
on SQL query string will do), serialize and deserialize query result.

Considering that query results may have different structure, I suppose that utilization of
would be a good candidate for serialization. But how to deserialize it in generic way so that code does not depend on result structure ?

The solution can use Sql 2008 features if needed.