Dapper Learning (II) of the related Query

 

0. FIrst , Single & Default

Be careful when using this method, First and Single is different.

 Here, at this table do Description:

If First, when the element is not found, an error will be; found if an element, the element will return; if a plurality of elements found, returns the first element;

If you use a Single, found no error will elements; found in an element, the element is returned; found multiple elements, then an error;

If you use FirstOrDefault, found no element will return the default value; if found in an element, the element is returned; if found multiple elements, the first element is returned;

If the SingleOrDefault, the default value for an element not found; if a found element, which is returned; if a plurality of elements found, then an error;

1. QueryFirst

It can be used to query and mapping the first result

The results can be mapped to:

  • Anonymous
  • Strongly Typed

parameter

1.1 Query Anonymous

Execute a query, and the first result is mapped to a dynamic collection

string sql = "SELECT * FROM OrderDetails WHERE OrderDetailID = @OrderDetailID;";

using (var connection = new SqlConnection(FiddleHelper.GetConnectionStringSqlServerW3Schools()))
{
    var orderDetail = connection.QueryFirst(sql, new {OrderDetailID = 1});

    FiddleHelper.WriteTable(orderDetail);
}

1.2 Query Strongly Typed

Execute a query, and the first result is mapped to a strongly typed collection

string sql = "SELECT * FROM OrderDetails WHERE OrderDetailID = @OrderDetailID;";

using (var connection = new SqlConnection(FiddleHelper.GetConnectionStringSqlServerW3Schools()))
{
    var orderDetail = connection.QueryFirst<OrderDetail>(sql, new {OrderDetailID = 1});

    FiddleHelper.WriteTable(orderDetail);
}

2. QueryFirstOrDefault

Execute a query, and the first results of the mapping, or if the element is not found, a default value is returned

The results can be mapped to:

  • Anonymous
  • Strongly Typed

parameter

 

 

 2.1 Query Anonymous

Execute a query, and the first result is mapped to a dynamic collection, or if the element is not found, a default value is returned

string sql = "SELECT * FROM OrderDetails WHERE OrderDetailID = @OrderDetailID;";

using (var connection = new SqlConnection(FiddleHelper.GetConnectionStringSqlServerW3Schools()))
{            
    var orderDetail = connection.QueryFirstOrDefault(sql, new {OrderDetailID = 1});

    FiddleHelper.WriteTable(orderDetail);
}

2.2 Query Strongly Typed

Execute a query, and the first result is mapped to a strongly typed collection, or if the element is not found, a default value is returned

string sql = "SELECT * FROM OrderDetails WHERE OrderDetailID = @OrderDetailID;";

using (var connection = new SqlConnection(FiddleHelper.GetConnectionStringSqlServerW3Schools()))
{
    var orderDetail = connection.QueryFirstOrDefault<OrderDetail>(sql, new {OrderDetailID = 1});

    FiddleHelper.WriteTable(new List<OrderDetail>() { orderDetail });
}

3. QuerySingle

Execute the query and mapping the first result, if found in more than one element, an exception is thrown

The results can be mapped to:

  • Anonymous
  • Strongly Typed

parameter

 

 

 3.1 Query Anonymous

Query is executed, the first result is mapped to a dynamic collection, if found in more than one element, an exception is thrown

string sql = "SELECT * FROM OrderDetails WHERE OrderDetailID = @OrderDetailID;";

using (var connection = new SqlConnection(FiddleHelper.GetConnectionStringSqlServerW3Schools()))
{    
    var orderDetail = connection.QuerySingle(sql, new {OrderDetailID = 1});

    FiddleHelper.WriteTable(orderDetail);
}

3.2 Query Strongly Typed

Query is executed, and the first result is mapped to a strongly typed collection, if found in more than one element, an exception is thrown

string sql = "SELECT * FROM OrderDetails WHERE OrderDetailID = @OrderDetailID;";

using (var connection = new SqlConnection(FiddleHelper.GetConnectionStringSqlServerW3Schools()))
{            
    var orderDetail = connection.QuerySingle<OrderDetail>(sql, new {OrderDetailID = 1});

    FiddleHelper.WriteTable(new List<OrderDetail>() { orderDetail });
}

4. QuerySingleOrDefault

Execute the query and mapping the first result, or if the query result is empty, the default value is returned; if found in more than one element, an exception is thrown

The results are mapped to:

  • Anonymous
  • Strongly Typed

parameter

 

 

 4.1 Query Anonymous

Query execution, and a mapping result to a first dynamic collection, or if the query result is empty, a default value is returned; if more than one element found, an exception is thrown.

string sql = "SELECT * FROM OrderDetails WHERE OrderDetailID = @OrderDetailID;";

using (var connection = new SqlConnection(FiddleHelper.GetConnectionStringSqlServerW3Schools()))
{
    var orderDetail = connection.QuerySingleOrDefault(sql, new {OrderDetailID = 1});

    FiddleHelper.WriteTable(orderDetail);
}

4.2 Query Strongly Typed

Query execution, and a mapping of the first result to a strongly typed collection, or if the query result is empty, a default value is returned; if more than one element found, an exception is thrown

string sql = "SELECT * FROM OrderDetails WHERE OrderDetailID = @OrderDetailID;";

using (var connection = new SqlConnection(FiddleHelper.GetConnectionStringSqlServerW3Schools()))
{            
    var orderDetail = connection.QuerySingleOrDefault<OrderDetail>(sql, new {OrderDetailID = 1});

    FiddleHelper.WriteTable(new List<OrderDetail>() { orderDetail });
}

5. QueryMultiple

It can perform multiple queries in the same command, and mapped to results

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

 

Guess you like

Origin www.cnblogs.com/Vincent-yuan/p/11515154.html