Dapper to DataTable

c# dapper

Question

I have a scenario where I need to return a DataTable from a query using Dapper. How do I return a DataTable from a query using Dapper?

DataTable dt = connection.Query("SELECT * FROM table");

Accepted Answer

There will be no advantage whatsoever in using dapper for a scenario involving DataSet. And in particular, your specific example (without any parameters etc) is so trivial (not meant negatively - simply objectively) that you might as well use ExecuteReader directly, or use a DbDataAdapter

I am, however, open to exposing an API on dapper that exposes the IDataReader API from dapper - you could feed that to any consumer you want, DataSet / DataTable included. But I really must question: what would be the point in performing this example via dapper? It might make more sense if you were at least using dapper to handle parameters (I'm damned pleased with how the parameter handling worked out, truth be told).


Popular Answer

Huh? Dapper only provides extension methods over ADO.NET - so that you don't have to deal with DataTables and DataSets!

If DataTables and DataSets are what you want - you can still use vanilla ADO.NET - SqlDataAdapter (http://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqldataadapter.aspx) will give you all the DataTables your heart desires :)




Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Is this KB legal? Yes, learn why
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Is this KB legal? Yes, learn why