How do I handle Database Connections with Dapper in .NET?

Update: clarification from MarredCheese’s comment: “No need to use a using statement. Dapper will automatically open, close, and dispose of the connection for you.” That’s not correct. Dapper will automatically open closed connections, and it will automatically close connections that it auto-opened, but it will not automatically dispose of connections. Marc Gravell and Eric Lippert … Read more

Adjusting CommandTimeout in Dapper.NET?

Yes, there are multiple versions of the Execute function. One (or more) of them contains the commandTimeout parameters: public static int Execute(this IDbConnection cnn, string sql, dynamic param = null, IDbTransaction transaction = null, int? commandTimeout = null, CommandType? commandType = null) Taken from SqlMapper.cs

Correct use of multimapping in Dapper

I just ran a test that works fine: var sql = “select cast(1 as decimal) ProductId, ‘a’ ProductName, ‘x’ AccountOpened, cast(1 as decimal) CustomerId, ‘name’ CustomerName”; var item = connection.Query<ProductItem, Customer, ProductItem>(sql, (p, c) => { p.Customer = c; return p; }, splitOn: “CustomerId”).First(); item.Customer.CustomerId.IsEqualTo(1); The splitOn param needs to be specified as the split … Read more

How do I map lists of nested objects with Dapper

Alternatively, you can use one query with a lookup: var lookup = new Dictionary<int, Course>(); conn.Query<Course, Location, Course>(@” SELECT c.*, l.* FROM Course c INNER JOIN Location l ON c.LocationId = l.Id “, (c, l) => { Course course; if (!lookup.TryGetValue(c.Id, out course)) lookup.Add(c.Id, course = c); if (course.Locations == null) course.Locations = new List<Location>(); … 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

How do I perform an insert and return inserted identity with Dapper?

It does support input/output parameters (including RETURN value) if you use DynamicParameters, but in this case the simpler option is simply: var id = connection.QuerySingle<int>( @” INSERT INTO [MyTable] ([Stuff]) VALUES (@Stuff); SELECT CAST(SCOPE_IDENTITY() as int)”, new { Stuff = mystuff}); Note that on more recent versions of SQL Server (2005+) you can use the … Read more

Is there a way to call a stored procedure with Dapper?

In the simple case you can do: var user = cnn.Query<User>(“spGetUser”, new {Id = 1}, commandType: CommandType.StoredProcedure).First(); If you want something more fancy, you can do: 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); cnn.Execute(“spMagicProc”, p, commandType: CommandType.StoredProcedure); int b = p.Get<int>(“@b”); int c = p.Get<int>(“@c”); … Read more

Performing Inserts and Updates with Dapper

We are looking at building a few helpers, still deciding on APIs and if this goes in core or not. See: https://code.google.com/archive/p/dapper-dot-net/issues/6 for progress. In the mean time you can do the following val = “my value”; cnn.Execute(“insert into Table(val) values (@val)”, new {val}); cnn.Execute(“update Table set val = @val where Id = @id”, new … Read more

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