Dapper: Result from “SELECT COUNT(*) FROM TableName

Please don’t do this! It’s fragile, and introduces a gaping sql injection vulnerability. If you can return your count for a given table with one line of very expressive code, and no vulnerability, why make it method? Do this instead: DapperConnection.ExecuteScalar<int>(“SELECT COUNT(*) FROM customers”); // You will be happier and live longer if you avoid … Read more

Is there any way to trace\log the sql using Dapper?

I got the same issue and implemented some code after doing some search but having no ready-to-use stuff. There is a package on nuget MiniProfiler.Integrations I would like to share. Update V2: it supports to work with other database servers, for MySQL it requires to have MiniProfiler.Integrations.MySql Below are steps to work with SQL Server: … Read more

Simple inner join result with Dapper?

You can do something like this (see https://www.tritac.com/blog/dappernet-by-example): public class Shop { public int? Id {get;set;} public string Name {get;set;} public string Url {get;set;} public IList<Account> Accounts {get;set;} } public class Account { public int? Id {get;set;} public string Name {get;set;} public string Address {get;set;} public string Country {get;set;} public int ShopId {get;set;} } var … Read more

How to pass a null parameter with Dapper

I think you should be able to do it by casting null to the appropiate type. Let’s assume RouteId is an integer: connection.Execute(“spLMS_UpdateLMSLCarrier”, new { RouteId = (int?)null, CarrierId = carrierID, UserId = userID, TotalRouteRateCarrierId = totalRouteRateCarrierId }, commandType: CommandType.StoredProcedure); The problem you are encountering when using regular null is likely that the compiler cannot … Read more

Manually map column names with class properties

Dapper now supports custom column to property mappers. It does so through the ITypeMap interface. A CustomPropertyTypeMap class is provided by Dapper that can do most of this work. For example: Dapper.SqlMapper.SetTypeMap( typeof(TModel), new CustomPropertyTypeMap( typeof(TModel), (type, columnName) => type.GetProperties().FirstOrDefault(prop => prop.GetCustomAttributes(false) .OfType<ColumnAttribute>() .Any(attr => attr.Name == columnName)))); And the model: public class TModel { … Read more

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