Welcome, Guest |
TOPIC:
Dapper MicroORM to Query SQL Databases in .net/X# 08 Oct 2022 20:16 #24120
|
Hi, I personally use Entity Framework Core (EFCore) to access SQL Databases with .net. But sometimes, when I just need to execute some SQL selects to get some data, EFCore is overkill. In those cases, I use a MicroORM called Dapper. Dapper is a small library, written by StackOverflow, that does one thing: It adds a generic Query method to IDBConnection, that takes a select statement and returns a list of objects as a result very fast. The field values are assigned to the object properties based on name equality. Here is a small example that shows how to get data into a List using the "traditional" Command and Reader way and the Dapper way. class Product
property ID as int auto
property Name as string auto
end class
method WithQueryAndReader(connection as IDBConnection) as List<Product>
var result := List<Product>{}
begin using var command := connection:CreateCommand()
command:CommandText := "SELECT ID, Name FROM Product"
begin using var reader := command:ExecuteReader()
do while reader:Read()
var product := Product{}
product:ID := reader:GetInt32(reader.GetOrdinal("ID"))
product:Name := reader:GetString(reader.GetOrdinal("Name"))
end do
result:Add(product)
end using
end using
return result
method WithDapper(connection as IDBConnection) as List<Product>
return connection.Query<Product>("SELECT ID, Name FROM Product");
You can find more information on Dapper here: github.com/DapperLib/Dapper www.learndapper.com/ Dapper can be found on Nuget www.nuget.org/packages/Dapper Volkmar |
Please Log in or Create an account to join the conversation. |