Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dapper to DataTable [duplicate]

Tags:

c#

dapper

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");
like image 965
user2421145 Avatar asked Jul 19 '13 19:07

user2421145


People also ask

What is QueryAsync in dapper?

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).

Is Dapper case sensitive?

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.


2 Answers

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).

like image 72
Marc Gravell Avatar answered Nov 14 '22 13:11

Marc Gravell


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.

like image 6
Vivek Avatar answered Nov 14 '22 14:11

Vivek