Tutorial by Examples: c

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 ...
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...
public class IHtmlStringTypeHandler : SqlMapper.TypeHandler<IHtmlString> { public override void SetValue( IDbDataParameter parameter, IHtmlString value) { parameter.DbType = DbType.String; parameter.Value = value?.ToHtmlString(); } pu...
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...
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...
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...
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, ...
Initialize a collection type with values: var stringList = new List<string> { "foo", "bar", }; Collection initializers are syntactic sugar for Add() calls. Above code is equivalent to: var temp = new List<string>(); temp.Add("foo"); temp.A...
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...
Expression-bodied function members allow the use of lambda expressions as member bodies. For simple members, it can result in cleaner and more readable code. Expression-bodied functions can be used for properties, indexers, methods, and operators. Properties public decimal TotalPrice => Base...
Exception filters give developers the ability to add a condition (in the form of a boolean expression) to a catch block, allowing the catch to execute only if the condition evaluates to true. Exception filters allow the propagation of debug information in the original exception, where as using an...
It is possible to use await expression to apply await operator to Tasks or Task(Of TResult) in the catch and finally blocks in C#6. It was not possible to use the await expression in the catch and finally blocks in earlier versions due to compiler limitations. C#6 makes awaiting async tasks a lot e...
By definition, the short-circuiting boolean operators will only evaluate the second operand if the first operand can not determine the overall result of the expression. It means that, if you are using && operator as firstCondition && secondCondition it will evaluate secondCondition ...

Page 1 of 826