Tutorial by Examples

using StackExchange.Redis; // ... // connect to the server ConnectionMultiplexer connection = ConnectionMultiplexer.Connect("localhost"); // select a database (by default, DB = 0) IDatabase db = connection.GetDatabase(); // run a command, in this case a GET RedisValue myVal ...
Either search in the Visual Studio GUI: Tools > NuGet Package Manager > Manage Packages for Solution... (Visual Studio 2015) Or run this command in a Nuget Power Shell instance to install the latest stable version Install-Package Dapper Or for a specific version Install-Package Dapper...
For types known at compile-time, use a generic parameter with Query<T>. public class Dog { public int? Age { get; set; } public Guid Id { get; set; } public string Name { get; set; } public float? Weight { get; set; } public int IgnoredProperty { get { return 1; } ...
You can also query dynamically if you leave off the generic type. IDBConnection db = /* ... */; IEnumerable<dynamic> result = db.Query("SELECT 1 as A, 2 as B"); var first = result.First(); int a = (int)first.A; // 1 int b = (int)first.B; // 2
class ToyProfiler : IProfiler { public ConcurrentDictionary<Thread, object> Contexts = new ConcurrentDictionary<Thread, object>(); public object GetContext() { object ctx; if(!Contexts.TryGetValue(Thread.CurrentThread, out ctx)) ctx = null; ...
ConnectionMultiplexer conn = /* initialization */; var profiler = new ToyProfiler(); conn.RegisterProfiler(profiler); var threads = new List<Thread>(); var perThreadTimings = new ConcurrentDictionary<Thread, List<IProfiledCommand>>(); for (var i = 0; i < 16; i++) {...
IDBConnection db = /* ... */ var id = /* ... */ db.Execute(@"update dbo.Dogs set Name = 'Beowoof' where Id = @id", new { id });
Simple usage Dapper fully supports stored procs: var user = conn.Query<User>("spGetUser", new { Id = 1 }, commandType: CommandType.StoredProcedure) .SingleOrDefault(); Input, Output and Return parameters If you want something more fancy...
using System.Data; using System.Linq; using Dapper; class Program { static void Main() { using (IDbConnection db = new SqlConnection("Server=myServer;Trusted_Connection=true")) { db.Open(); var result = db.Query<string>(&quo...
LINQPad is great for testing database queries and includes NuGet integration. To use Dapper in LINQPad press F4 to open the Query Properties and then select Add NuGet. Search for dapper dot net and select Add To Query. You will also want to click Add namespaces and highlight Dapper to include the Ex...
public class IHtmlStringTypeHandler : SqlMapper.TypeHandler<IHtmlString> { public override void SetValue( IDbDataParameter parameter, IHtmlString value) { parameter.DbType = DbType.String; parameter.Value = value?.ToHtmlString(); } pu...
To fetch multiple grids in a single query, the QueryMultiple method is used. This then allows you to retrieve each grid sequentially through successive calls against the GridReader returned. var sql = @"select * from Customers where CustomerId = @id select * from Orders where Cust...
Dapper makes it easy to follow best practice by way of fully parameterized SQL. Parameters are important, so dapper makes it easy to get it right. You just express your parameters in the normal way for your RDBMS (usually @foo, ?foo or :foo) and give dapper an object that has a member called foo...
Sometimes the convenience of a parameter (in terms of maintenance and expressiveness), may be outweighed by its cost in performance to treat it as a parameter. For example, when page size is fixed by a configuration setting. Or a status value is matched to an enum value. Consider: var orders = conn...
A common scenario in database queries is IN (...) where the list here is generated at runtime. Most RDBMS lack a good metaphor for this - and there is no universal cross-RDBMS solution for this. Instead, dapper provides some gentle automatic command expansion. All that is requires is a supplied para...
Sometimes, you want to do the same thing multiple times. Dapper supports this on the Execute method if the outermost parameter (which is usually a single anonymous type, or a domain model instance) is actually provided as an IEnumerable sequence. For example: Order[] orders = ... // update the tot...
All values in Redis are ultimately stored as a RedisValue type: //"myvalue" here is implicitly converted to a RedisValue type //The RedisValue type is rarely seen in practice. db.StringSet("key", "aValue");
db.StringSet("key", 11021); int i = (int)db.StringGet("key"); Or using StackExchange.Redis.Extensions: db.Add("key", 11021); int i = db.Get<int>("key");
It isn't always possible to neatly package all the parameters up in a single object / call. To help with more complicated scenarios, dapper allows the param parameter to be an IDynamicParameters instance. If you do this, your custom AddParameters method is called at the appropriate time and handed t...
In ADO.NET, correctly handling null is a constant source of confusion. The key point in dapper is that you don't have to; it deals with it all internally. parameter values that are null are correctly sent as DBNull.Value values read that are null are presented as null, or (in the case of mapping...

Page 1 of 1336