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");
The provider is just an abstraction on creating a database connection (SqlConnection) and returns an IDbConnection. We use that connection to call the QueryAsync method. QueryAsync is exactly what it sounds like, it asynchronously queries your database and maps it back to an object (in this case a Car object).
Dapper's(SqlMapper) Query method over the Connection factory runs the SQL query, then maps the database result to Employee class and returns as a list of employees. Note : Only matching class and table properties are mapped to list of employee, they are case sensitive.
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).
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 will give you all the DataTables your heart desires.
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