Dapper official document (5) [Method of QuerySingle, QuerySingleOrDefault, QueryMultiple]

QuerySingle

description

QuerySingleIt is an IDbConnectionextension method that can be called from any object of the type. It can execute a query and map the first result. If there is no element in the sequence, an exception will be raised.

The results can be mapped to:

  • Anonymous type
  • Strong type

parameter

The following table shows the QuerySingledifferent parameters of the method.

name description
sql The query to be executed.
param Query parameters (default is null).
transaction The transaction to be used (default is null).
commandTimeout Command execution timeout period (default is null).
commandType Command type (default is null).

First, Single & Default

Pay attention to the correct method. The method of Firstsum Singleis very different.

result No items Have one item There are multiple
First Throw exception Current item the first item
Single Throw exception Current item Throw exception
FirstOrDefault Defaults Current item the first item
SingleOrDefault Defaults Current item Throw exception

Case-Query Anonymous Type

Execute the query and map the first result to the dynamic type list. If there are no elements in the sequence, an exception will be raised.

string sql = "SELECT * FROM Invoice WHERE InvoiceID = @InvoiceID;";

using (var connection = My.ConnectionFactory())
{
    
    
    connection.Open();

    var invoice = connection.QuerySingle(sql, new {
    
    InvoiceID = 1});
}

Case-Query strong type

Execute the query and map the first result to a strongly typed list. If there are no elements in the sequence, an exception will be raised.

string sql = "SELECT * FROM Invoice WHERE InvoiceID = @InvoiceID;";

using (var connection = My.ConnectionFactory())
{
    
    
    connection.Open();

    var invoice = connection.QuerySingle<Invoice>(sql, new {
    
    InvoiceID = 1});
}

QuerySingleOrDefault

description

QuerySingleOrDefaultIt is an IDbConnectionextension method that can be called from any object of type. It can execute a query and map the first result. If the sequence is empty, it will be the default value. If there are multiple elements in the sequence, this method will throw an exception.

The results can be mapped to:

  • Anonymous type
  • Strong type

parameter

The following table shows the QuerySingleOrDefaultdifferent parameters of the method.

name description
sql The query to be executed.
param Query parameters (default is null).
transaction The transaction to be used (default is null).
commandTimeout Command execution timeout period (default is null).
commandType Command type (default is null).

First, Single & Default

Pay attention to the correct method. The method of Firstsum Singleis very different.

result No items Have one item There are multiple
First Throw exception Current item the first item
Single Throw exception Current item Throw exception
FirstOrDefault Defaults Current item the first item
SingleOrDefault Defaults Current item Throw exception

Case-Query Anonymous Type

Execute the query and map the first result to the dynamic type list, if the sequence is empty, it will be the default value. If there are multiple elements in the sequence, this method will throw an exception.

string sql = "SELECT * FROM Invoice WHERE InvoiceID = @InvoiceID;";

using (var connection = My.ConnectionFactory())
{
    
    
    connection.Open();

    var invoice = connection.QuerySingleOrDefault(sql, new {
    
    InvoiceID = 1});
}

Case-Query strong type

Execute the query and map the first result to a strongly typed list, if the sequence is empty, it will be the default value. If there are multiple elements in the sequence, this method will throw an exception.

string sql = "SELECT * FROM Invoice WHERE InvoiceID = @InvoiceID;";

using (var connection = My.ConnectionFactory())
{
    
    
    connection.Open();

    var invoice = connection.QuerySingleOrDefault<Invoice>(sql, new {
    
    InvoiceID = 1});
}

QueryMultiple

description

QueryMultipleIt is an IDbConnectionextension method that can be called from any object of the type, and it can execute multiple queries in the same command and mapping result.

string sql = "SELECT * FROM Invoice WHERE InvoiceID = @InvoiceID; SELECT * FROM InvoiceItem WHERE InvoiceID = @InvoiceID;";

using (var connection = My.ConnectionFactory())
{
    
    
    connection.Open();

    using (var multi = connection.QueryMultiple(sql, new {
    
    InvoiceID = 1}))
    {
    
    
        var invoice = multi.Read<Invoice>().First();
        var invoiceItems = multi.Read<InvoiceItem>().ToList();
    }
}

parameter

The following table shows the QueryMultipledifferent parameters of the method.

name description
sql The query to be executed.
param Query parameters (default is null).
transaction The transaction to be used (default is null).
commandTimeout Command execution timeout period (default is null).
commandType Command type (default is null).

Guess you like

Origin blog.csdn.net/WuLex/article/details/108316921