Dapper official translation Tutorial 6: QuerySingle Dapper method of
QuerySingle method described
QuerySingle method is an extension method, it can be called IDbConnection object, you can execute a query, and mapped to the result. If only one value, this value is returned directly, if there is no value, the exception will occur if more than one value, an exception will occur.
You can be mapped type:
The method may be used parameter QuerySingle
Name | Description |
---|---|
sql | check sentence |
param | parameter |
transaction | Whether to open the transaction |
commandTimeout | Timeout period |
commandType | Command Type |
Anonymous queries
-
string sql = "SELECT * FROM OrderDetails WHERE OrderDetailID = @OrderDetailID;";
-
-
using (var connection = new SqlCeConnection("Data Source=SqlCe_W3Schools.sdf"))
-
{
-
var orderDetail = connection.QuerySingle(sql, new {OrderDetailID = 1});
-
-
FiddleHelper.WriteTable(orderDetail);
-
}
Strongly typed queries
-
string sql = "SELECT * FROM OrderDetails WHERE OrderDetailID = @OrderDetailID;";
-
-
using (var connection = new SqlCeConnection("Data Source=SqlCe_W3Schools.sdf"))
-
{
-
var orderDetail = connection.QuerySingle<OrderDetail>(sql, new {OrderDetailID = 1});
-
-
FiddleHelper.WriteTable( new List<OrderDetail>() { orderDetail });
-
}