Explanation of dapper buffer/cache

The buffer is unrelated to cache. Dapper does not include any kind of data-cache (although it does have a cache related to how it processes commands, i.e. “this command string, with this type of parameter, and this type of entity – has these associated dynamically generated methods to configure the command and populate the objects”). … Read more

Dapper and SQL Injections

How does Dapper help protect against SQL injections? It makes it really, really easy to do fully parameterized data access, without ever needing to either concatenate input. In particular, because you don’t need to jump through lots of “add parameter, set the parameter type, check for null because ADO.NET has sucky null-handling, rinse/repeat for 20 … Read more

Dapper & TransactionScope?

It was totally my fault and not fully understanding transactionscope. A connection is not automatically enlisted in transactionscope unless you open the connection within the transactionscope: Automatic Enlistment using (var scope = new TransactionScope()) { con.Open(); //update/delete/insert commands here … scope.Complete(); } Manual Enlistment con.Open(); using (var scope = new TransactionScope()) { con.EnlistTransaction(Transaction.Current); //update/delte/insert statements … Read more

Can I map a result to Tuple in Dapper?

This works starting from C# 7. This is a Value Tuple public (int Id, DateTime? PublishDate) GetItem(string id) { const string sqlCommand = “select top 1 Id, PublishDate from Item where Id = @id”; return _connection.Query<(int, DateTime?)>(sqlCommand, new { id }).FirstOrDefault(); } Using the method var item = GetItem(123); Console.WriteLine($”The publish date of item [{item.Id}] … Read more

Multiple SQL statements in one roundtrip using Dapper.NET

Yes, the Dapper QueryMultiple extension can do that: string query = @”SELECT COUNT(*) FROM TABLEA; SELECT COUNT(*) FROM TABLEB”; using (var multi = connection.QueryMultiple(query, null)) { int countA = multi.Read<int>().Single(); int countB = multi.Read<int>().Single(); } According to Marc Gravell this is the ideal way to execute multiple queries in a single batch. Note: Dapper creator … Read more

Dapper. Paging

You didn’t specify a database or version. If you’re lucky enough to be able to use the brand new SQL Server 2012 and have access to MSDN, you can use the shiny new OFFSET and FETCH keywords. The following query will skip 20 records and return the next 5. SELECT * FROM [Posts] ORDER BY … Read more

Get DateTime as UTC with Dapper

Adding this answer for anyone else who comes looking for a simple fix. This is possible now with the addition of SqlMapper.TypeHandler in Dapper. Add this class to convert the value from the db to a datetime with the kind specified as UTC. public class DateTimeHandler : SqlMapper.TypeHandler<DateTime> { public override void SetValue(IDbDataParameter parameter, DateTime … Read more

Passing Output parameters to stored procedure using dapper in c# code

Just searching the Test.cs file you could find this example public void TestProcSupport() { var p = new DynamicParameters(); p.Add(“a”, 11); p.Add(“b”, dbType: DbType.Int32, direction: ParameterDirection.Output); p.Add(“c”, dbType: DbType.Int32, direction: ParameterDirection.ReturnValue); connection.Execute(@”create proc #TestProc @a int, @b int output as begin set @b = 999 select 1111 return @a end”); connection.Query<int>(“#TestProc”, p, commandType: CommandType.StoredProcedure).First().IsEqualTo(1111); p.Get<int>(“c”).IsEqualTo(11); … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)