Tutorial by Examples: d

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...
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...
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...
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");
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...
Open Visual Studio In the toolbar, go to File → New Project Select the Console Application project type Open the file Program.cs in the Solution Explorer Add the following code to Main(): public class Program { public static void Main() { // Prints a message to the conso...
using System; class Program { // The Main() function is the first function to be executed in a program static void Main() { // Write the string "Hello World to the standard out Console.WriteLine("Hello World"); } } Console.WriteLine has se...
string fullOrRelativePath = "testfile.txt"; string fileData; using (var reader = new StreamReader(fullOrRelativePath)) { fileData = reader.ReadToEnd(); } Note that this StreamReader constructor overload does some auto encoding detection, which may or may not conform to the ...
C# allows user-defined types to overload operators by defining static member functions using the operator keyword. The following example illustrates an implementation of the + operator. If we have a Complex class which represents a complex number: public struct Complex { public double Real ...
Extension methods were introduced in C# 3.0. Extension methods extend and add behavior to existing types without creating a new derived type, recompiling, or otherwise modifying the original type. They are especially helpful when you cannot modify the source of a type you are looking to enhance. Ex...
Extension methods can also be used like ordinary static class methods. This way of calling an extension method is more verbose, but is necessary in some cases. static class StringExtensions { public static string Shorten(this string text, int length) { return text.Substring(0, ...
var date = new DateTime(2015, 11, 11); var str = $"It's {date:MMMM d, yyyy}, make a wish!"; System.Console.WriteLine(str); You can also use the DateTime.ToString method to format the DateTime object. This will produce the same output as the code above. var date = new DateTime(2015, 1...
Double Quotes inside verbatim strings can be escaped by using 2 sequential double quotes "" to represent one double quote " in the resulting string. var str = @"""I don't think so,"" he said."; Console.WriteLine(str); Output: "I don't think s...
Verbatim strings can be combined with the new String interpolation features found in C#6. Console.WriteLine($@"Testing \n 1 2 {5 - 2} New line"); Output: Testing \n 1 2 3 New line Live Demo on .NET Fiddle As expected from a verbatim string, the backslashes are ignored as escap...

Page 1 of 691