Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

is there an ExecuteScalar in Dapper

Tags:

c#

dapper

it looks like there was an ExecuteScalar in Dapper...

http://code.google.com/p/dapper-dot-net/issues/attachmentText?id=22&aid=220000000&name=ExecuteScalar.cs&token=9e2fd8899022f507b140ffb883c60e34

Was ExecuteScalar renamed or removed?

Can this now be achieved with .Query or .Query<T>?

like image 678
sgtz Avatar asked Nov 08 '11 12:11

sgtz


People also ask

What is dapper ExecuteScalar?

ExecuteScalar<T> Returns an instance of the type specified by the T type parameter. ExecuteScalarAsync. Returns a dynamic type asynchronously.

What is DynamicParameters in dapper?

The DynamicParameters type provides an Add method, enabling you to pass explicit parameters, specifying the datatype, direction and size: var parameters = new DynamicParameters(); var customerId = "ALFKI"; parameters. Add("@CustomerId", customerId, DbType.

What databases does dapper support?

Dapper has no DB specific implementation details, it works across all . NET ADO providers including SQLite, SQL CE, Firebird, Oracle, MySQL, PostgreSQL and SQL Server. Dapper was created by team at Stack Overflow. To utilize Dapper, we add the package reference to the project with the dotnet tool.


3 Answers

ExecuteScalar was just added in 1.28: https://www.nuget.org/packages/Dapper

like image 161
Evan M Avatar answered Nov 12 '22 16:11

Evan M


I was able to call ExecuteScalar< T > with version 1.42.0

    public Boolean BeforeToday(DateTime dateInQuestion)
    {
        try
        {
            using (var conn = new SqlConnection(ConnectionString))
            {
                String sql = @"SELECT CONVERT(bit, CASE WHEN getdate() > @dateParameter THEN 1 ELSE 0 END) AS BeforeToday";

                var result = conn.ExecuteScalar<Boolean>(sql, new { dateParameter = dateInQuestion });

                return result;
            }
        }
        catch (Exception)
        {
            return dateInQuestion < DateTime.Now;
        }
    }
like image 34
Doug Dekker Avatar answered Nov 12 '22 16:11

Doug Dekker


In version 1.50.4 I was able to call connection.QuerySingle<int>(query,params)

like image 44
N.Rybchenko Avatar answered Nov 12 '22 16:11

N.Rybchenko