### Install Latest Pre-release Dapper via NuGet Source: https://github.com/dapperlib/dapper/blob/main/docs/index.md Add the '-Pre' flag to install the latest pre-release version of the Dapper NuGet package. ```powershell Install-Package Dapper -Pre ``` -------------------------------- ### Example Users Table Schema Source: https://github.com/dapperlib/dapper/blob/main/Dapper.Rainbow/readme.md Defines a sample 'Users' table with an 'Id' primary key, 'Name', and 'Email' columns. ```sql CREATE TABLE Users ( Id INT IDENTITY(1,1) PRIMARY KEY, Name VARCHAR(100), Email VARCHAR(100) ); ``` -------------------------------- ### Install Dapper StrongName via NuGet Package Manager Console Source: https://github.com/dapperlib/dapper/blob/main/docs/index.md Use this command to install the Dapper.StrongName NuGet package using the Package Manager Console in Visual Studio. ```powershell Install-Package Dapper.StrongName ``` -------------------------------- ### Install Dapper via NuGet Package Manager Console Source: https://github.com/dapperlib/dapper/blob/main/docs/index.md Use this command to install the Dapper NuGet package using the Package Manager Console in Visual Studio. ```powershell Install-Package Dapper ``` -------------------------------- ### Install Dapper and Dapper.Rainbow NuGet Packages Source: https://github.com/dapperlib/dapper/blob/main/Dapper.Rainbow/readme.md Add the necessary Dapper and Dapper.Rainbow packages to your project using the NuGet Package Manager Console. ```powershell Install-Package Dapper -Version x.x.x Install-Package Dapper.Rainbow -Version x.x.x ``` -------------------------------- ### Example Posts Table Schema with Foreign Key Source: https://github.com/dapperlib/dapper/blob/main/Dapper.Rainbow/readme.md Defines a 'Posts' table with an 'Id', a 'UserId' foreign key referencing the 'Users' table, and 'Content'. ```sql CREATE TABLE Posts ( Id INT IDENTITY(1,1) PRIMARY KEY, UserId INT, Content VARCHAR(255), FOREIGN KEY (UserId) REFERENCES Users(Id) ); ``` -------------------------------- ### Equivalent SQL without Dapper.SqlBuilder Source: https://github.com/dapperlib/dapper/blob/main/Dapper.SqlBuilder/Readme.md Shows the equivalent SQL query and parameter object that would be generated by the Dapper.SqlBuilder example above, for comparison. ```csharp var count = cnn.Query("select count(*) from table where a = @a and b = @b", new { a = 1, b = 1 }); var rows = cnn.Query("select * from table where a = @a and b = @b order by a, b", new { a = 1, b = 1 }); ``` -------------------------------- ### Dapper.SqlBuilder.Template.RawSql Source: https://github.com/dapperlib/dapper/blob/main/Dapper.SqlBuilder/PublicAPI.Shipped.txt Gets the raw SQL string of the template. ```APIDOC ## Dapper.SqlBuilder.Template.RawSql.get ### Description Gets the raw SQL string of the template. ### Property RawSql ### Signature Dapper.SqlBuilder.Template.RawSql.get -> string! ### Returns - string! - The raw SQL string. ``` -------------------------------- ### Select User by ID or All Users Source: https://github.com/dapperlib/dapper/blob/main/Dapper.Rainbow/readme.md Demonstrates fetching a single user by their ID using 'Get' and retrieving all users from the 'Users' table using Dapper's 'Query'. ```csharp var user = db.Users.Get(id); // Single user by ID var users = connection.Query("SELECT * FROM Users"); // All users ``` -------------------------------- ### Dapper.SqlBuilder.Template.Parameters Source: https://github.com/dapperlib/dapper/blob/main/Dapper.SqlBuilder/PublicAPI.Shipped.txt Gets the parameters associated with the SQL template. ```APIDOC ## Dapper.SqlBuilder.Template.Parameters.get ### Description Gets the parameters associated with the SQL template. ### Property Parameters ### Signature Dapper.SqlBuilder.Template.Parameters.get -> object? ### Returns - object? - The parameters for the template. ``` -------------------------------- ### Dynamic Filter Paging with Dapper.SqlBuilder Source: https://github.com/dapperlib/dapper/blob/main/Dapper.SqlBuilder/Readme.md Example of using Dapper.SqlBuilder for dynamic filtering and paging. It constructs a select template with ROW_NUMBER() for pagination and a count template, applying filters and ordering dynamically. ```csharp var builder = new SqlBuilder(); var selectTemplate = builder.AddTemplate(@ ``` -------------------------------- ### OrWhere Clause Example Source: https://github.com/dapperlib/dapper/blob/main/Dapper.SqlBuilder/Readme.md Demonstrates how to use the OrWhere clause in conjunction with Where clauses. SqlBuilder generates the combined SQL statement. ```csharp sql.OrWhere("b = @b1"); sql.Where("a = @a1"); sql.OrWhere("b = @b2"); sql.Where("a = @a2"); ``` ```sql a = @a1 OR a = @a2 OR ( b = @b1 OR b = @b2 ) ``` -------------------------------- ### Combining Where and OrWhere Clauses in Dapper.SqlBuilder Source: https://github.com/dapperlib/dapper/blob/main/Dapper.SqlBuilder/Readme.md Illustrates the behavior of combining Where and OrWhere methods in Dapper.SqlBuilder. The example shows how clauses are grouped and combined with 'AND' or 'OR' based on the initial call, potentially leading to unexpected SQL structure. ```sql a = @a1 AND a = @a2 AND ( b = @b1 OR b = @b2 ) ``` -------------------------------- ### Running Dapper Performance Benchmarks Source: https://github.com/dapperlib/dapper/blob/main/Readme.md Instructions on how to run performance benchmarks for Dapper using the .NET CLI. This helps in understanding the library's efficiency under various configurations. ```bash dotnet run --project .\benchmarks\Dapper.Tests.Performance\ -c Release -f net8.0 -- -f * --join ``` -------------------------------- ### Composing SQL Templates with Dapper.SqlBuilder Source: https://github.com/dapperlib/dapper/blob/main/Dapper.SqlBuilder/Readme.md Demonstrates how to use SqlBuilder to create and execute SQL templates. It shows how to add WHERE and ORDER BY clauses, and then use AddTemplate to generate raw SQL and parameters for Dapper's Query method. ```csharp var builder = new SqlBuilder() .Where("a = @a", new { a = 1 }) .Where("b = @b", new { b = 2 }) .OrderBy("a") .OrderBy("b"); var counter = builder.AddTemplate("select count(*) from table /**where**/", new { }); var selector = builder.AddTemplate("select * from table /**where**/ /**orderby**/", new { }); var count = cnn.Query(counter.RawSql, counter.Parameters).Single(); var rows = cnn.Query(selector.RawSql, selector.Parameters); ``` -------------------------------- ### Dapper.SqlBuilder.SqlBuilder Constructor Source: https://github.com/dapperlib/dapper/blob/main/Dapper.SqlBuilder/PublicAPI.Shipped.txt Initializes a new instance of the Dapper.SqlBuilder class. ```APIDOC ## Dapper.SqlBuilder.SqlBuilder() ### Description Initializes a new instance of the Dapper.SqlBuilder class. ### Method Constructor ### Signature Dapper.SqlBuilder.SqlBuilder() -> void ``` -------------------------------- ### GetTypeName Source: https://github.com/dapperlib/dapper/blob/main/Dapper/PublicAPI.Shipped.txt Gets the type name of a DataTable. ```APIDOC ## GetTypeName ### Description Retrieves the name of a `DataTable`. This is an extension method. ### Method static ### Signature Dapper.SqlMapper.GetTypeName(this System.Data.DataTable table) ### Parameters - **table** (DataTable) - The `DataTable` instance. ### Returns string? - The name of the `DataTable`, or null if it has no name. ``` -------------------------------- ### Establish Database Connection Source: https://github.com/dapperlib/dapper/blob/main/Dapper.Rainbow/readme.md Opens a SqlConnection using a provided connection string. Ensure the connection is properly managed (e.g., using 'using'). ```csharp using System.Data.SqlClient; var connectionString = "your_connection_string_here"; using var connection = new SqlConnection(connectionString); connection.Open(); // Open the connection ``` -------------------------------- ### GetCachedSQLCount Source: https://github.com/dapperlib/dapper/blob/main/Dapper/PublicAPI.Shipped.txt Gets the total number of cached SQL statements. ```APIDOC ## GetCachedSQLCount ### Description Returns the total number of SQL statements currently stored in Dapper's query cache. ### Method static ### Signature Dapper.SqlMapper.GetCachedSQLCount() ### Returns int - The total count of cached SQL statements. ``` -------------------------------- ### Define Database Context and User Model Source: https://github.com/dapperlib/dapper/blob/main/Dapper.Rainbow/readme.md Sets up a 'MyDatabase' context class inheriting from 'Database' and defines a 'User' model class with properties corresponding to table columns. ```csharp using Dapper; using System.Data; public class MyDatabase : Database { public Table Users { get; set; } } public class User { public int Id { get; set; } public string Name { get; set; } public string Email { get; set; } } ``` -------------------------------- ### PackListParameters Source: https://github.com/dapperlib/dapper/blob/main/Dapper/PublicAPI.Shipped.txt Packs list parameters for use in SQL commands. ```APIDOC ## PackListParameters ### Description This method is used to pack parameters that represent lists or collections into a format suitable for SQL commands, often by creating temporary tables or using table-valued parameters. This is an internal Dapper utility. ### Method static ### Signature Dapper.SqlMapper.PackListParameters(System.Data.IDbCommand command, string namePrefix, object value) ### Parameters - **command** (IDbCommand) - The `IDbCommand` to which the packed parameters will be added. - **namePrefix** (string) - A prefix for the names of the generated parameters. - **value** (object) - The list or collection of values to pack. ``` -------------------------------- ### Dapper.SqlBuilder.Template Constructor Source: https://github.com/dapperlib/dapper/blob/main/Dapper.SqlBuilder/PublicAPI.Shipped.txt Initializes a new instance of the Template class. ```APIDOC ## Dapper.SqlBuilder.Template(Dapper.SqlBuilder builder, string sql, dynamic parameters) ### Description Initializes a new instance of the Template class. ### Method Constructor ### Signature Dapper.SqlBuilder.Template(Dapper.SqlBuilder builder, string sql, dynamic parameters) -> void ### Parameters - **builder** (Dapper.SqlBuilder) - Required - The SQL builder instance. - **sql** (string) - Required - The SQL template string. - **parameters** (dynamic) - Required - Dynamic parameters for the template. ``` -------------------------------- ### Executing Commands Multiple Times with Existing Collection Source: https://github.com/dapperlib/dapper/blob/main/Readme.md Execute an SQL command multiple times using an existing collection of objects that implement `IEnumerable`. This is a convenient way to bulk-load data from pre-existing collections. ```csharp var foos = new List { { new Foo { A = 1, B = 1 } } { new Foo { A = 2, B = 2 } } { new Foo { A = 3, B = 3 } } }; var count = connection.Execute(@"insert MyTable(colA, colB) values (@a, @b)", foos); Assert.Equal(foos.Count, count); // 3 rows inserted: "1,1", "2,2" and "3,3" ``` -------------------------------- ### Core Dapper APIs Source: https://github.com/dapperlib/dapper/blob/main/Readme.md Provides the main extension methods for interacting with database connections. Use `Execute` for insert/update/delete, `Query` for multi-row results, and `QuerySingle` for single-row results. ```csharp // insert/update/delete etc var count = connection.Execute(sql [, args]); // multi-row query IEnumerable rows = connection.Query(sql [, args]); // single-row query ({Single|First}[OrDefault]) T row = connection.QuerySingle(sql [, args]); ``` -------------------------------- ### QuerySingle Source: https://github.com/dapperlib/dapper/blob/main/Dapper/PublicAPI.Shipped.txt Executes a query that returns a single, non-null result. Throws an exception if the query returns zero or more than one row. ```APIDOC ## QuerySingle ### Description Executes a query that returns a single, non-null result of type T. Throws an exception if the query returns zero or more than one row. ### Method `static Dapper.SqlMapper.QuerySingle(this System.Data.IDbConnection! cnn, string! sql, object? param = null, System.Data.IDbTransaction? transaction = null, int? commandTimeout = null, System.Data.CommandType? commandType = null) -> T` ### Parameters - **cnn** (System.Data.IDbConnection!) - The database connection. - **sql** (string!) - The SQL query to execute. - **param** (object?) - Optional parameters for the SQL query. - **transaction** (System.Data.IDbTransaction?) - Optional transaction to use. - **commandTimeout** (int?) - Optional command timeout in seconds. - **commandType** (System.Data.CommandType?) - Optional command type. ``` -------------------------------- ### Querying Customers by Region Source: https://github.com/dapperlib/dapper/blob/main/docs/readme.md This snippet demonstrates how to query a list of customers from a database based on a specified region using Dapper. It shows how to pass SQL queries and parameters to the `Query` method. ```csharp string region = ... var customers = connection.Query( "select * from Customers where Region = @region", // SQL new { region } // parameters ).AsList(); ``` -------------------------------- ### QueryAsync(CommandDefinition) Source: https://github.com/dapperlib/dapper/blob/main/Dapper/PublicAPI.Shipped.txt Asynchronously executes a query against the database using a CommandDefinition object and returns a collection of dynamic objects. ```APIDOC ## QueryAsync(CommandDefinition) ### Description Asynchronously executes a query against the database using a CommandDefinition object and returns a collection of dynamic objects. ### Method `Dapper.SqlMapper.QueryAsync(System.Data.IDbConnection, Dapper.CommandDefinition)` ### Parameters - `cnn` (System.Data.IDbConnection) - The database connection. - `command` (Dapper.CommandDefinition) - The command definition containing the query and parameters. ``` -------------------------------- ### Query (IDbConnection, string) Source: https://github.com/dapperlib/dapper/blob/main/Dapper/PublicAPI.Shipped.txt Executes a SQL query and returns a collection of dynamic objects. ```APIDOC ## Query (IDbConnection, string) ### Description Executes a SQL query against the provided database connection and returns the results as a sequence of dynamic objects. This is a fundamental method for executing read operations. ### Method static ### Signature Dapper.SqlMapper.Query(this System.Data.IDbConnection cnn, string sql, object? param = null, System.Data.IDbTransaction? transaction = null, bool buffered = true, int? commandTimeout = null, System.Data.CommandType? commandType = null) ### Parameters - **cnn** (IDbConnection) - The database connection to use. - **sql** (string) - The SQL query to execute. - **param** (object?) - Optional - Parameters for the SQL query. - **transaction** (IDbTransaction?) - Optional - The transaction to use for the query. - **buffered** (bool) - Optional - Whether to buffer the results in memory. Defaults to true. - **commandTimeout** (int?) - Optional - The command timeout in seconds. - **commandType** (CommandType?) - Optional - The type of the command (e.g., Text, StoredProcedure). ### Returns IEnumerable - An enumerable collection of dynamic objects representing the query results. ``` -------------------------------- ### QueryAsync(string, object?) Source: https://github.com/dapperlib/dapper/blob/main/Dapper/PublicAPI.Shipped.txt Asynchronously executes a query against the database and returns a collection of dynamic objects. ```APIDOC ## QueryAsync(string, object?) ### Description Asynchronously executes a query against the database and returns a collection of dynamic objects. ### Method `Dapper.SqlMapper.QueryAsync(System.Data.IDbConnection, string, object?)` ### Parameters - `cnn` (System.Data.IDbConnection) - The database connection. - `sql` (string) - The SQL query to execute. - `param` (object?) - Optional parameters for the query. - `transaction` (System.Data.IDbTransaction?) - Optional transaction. - `commandTimeout` (int?) - Optional command timeout in seconds. - `commandType` (System.Data.CommandType?) - Optional command type. ``` -------------------------------- ### Dapper.SqlBuilder Extension Methods Source: https://github.com/dapperlib/dapper/blob/main/Dapper.SqlBuilder/Readme.md Lists available extension methods for building SQL queries with Dapper.SqlBuilder. These methods help in constructing SELECT, WHERE, ORDER BY, GROUP BY, SET, and JOIN clauses. ```csharp SqlBuilder AddParameters(dynamic parameters); SqlBuilder Select(string sql, dynamic parameters = null); SqlBuilder Where(string sql, dynamic parameters = null); SqlBuilder OrWhere(string sql, dynamic parameters = null); SqlBuilder OrderBy(string sql, dynamic parameters = null); SqlBuilder GroupBy(string sql, dynamic parameters = null); SqlBuilder Having(string sql, dynamic parameters = null); SqlBuilder Set(string sql, dynamic parameters = null); SqlBuilder Join(string sql, dynamic parameters = null); SqlBuilder InnerJoin(string sql, dynamic parameters = null); SqlBuilder LeftJoin(string sql, dynamic parameters = null); SqlBuilder RightJoin(string sql, dynamic parameters = null); SqlBuilder Intersect(string sql, dynamic parameters = null); ``` -------------------------------- ### Insert a New User Record Source: https://github.com/dapperlib/dapper/blob/main/Dapper.Rainbow/readme.md Creates a new 'User' object and inserts it into the database using the 'Insert' method of the 'Users' table context. ```csharp var db = new MyDatabase { Connection = connection }; var newUser = new User { Name = "John Doe", Email = "john.doe@example.com" }; var insertedUser = db.Users.Insert(newUser); ``` -------------------------------- ### Executing Commands Multiple Times with Array of Parameters Source: https://github.com/dapperlib/dapper/blob/main/Readme.md Efficiently execute an SQL command multiple times using an array of parameter objects, suitable for bulk operations. The `Execute` method returns the total number of rows affected across all executions. ```csharp var count = connection.Execute(@"insert MyTable(colA, colB) values (@a, @b)", new[] { new { a=1, b=1 }, new { a=2, b=2 }, new { a=3, b=3 } } ); Assert.Equal(3, count); // 3 rows inserted: "1,1", "2,2" and "3,3" ``` -------------------------------- ### Querying Dynamic Objects Source: https://github.com/dapperlib/dapper/blob/main/Readme.md Execute a SQL query and retrieve results as a list of dynamic objects. This is useful when the structure of the returned data is not known beforehand or when dealing with heterogeneous results. ```csharp var rows = connection.Query("select 1 A, 2 B union all select 3, 4").AsList(); Assert.Equal(1, (int)rows[0].A); Assert.Equal(2, (int)rows[0].B); Assert.Equal(3, (int)rows[1].A); Assert.Equal(4, (int)rows[1].B); ``` -------------------------------- ### Translated SQL with List Parameters Source: https://github.com/dapperlib/dapper/blob/main/Readme.md This shows how Dapper translates a query with a list parameter into a SQL statement with individual parameters for each list element. ```sql select * from (select 1 as Id union all select 2 union all select 3) as X where Id in (@Ids1, @Ids2, @Ids3)" // @Ids1 = 1 , @Ids2 = 2 , @Ids2 = 3 ``` -------------------------------- ### Query (IDbConnection, CommandDefinition) Source: https://github.com/dapperlib/dapper/blob/main/Dapper/PublicAPI.Shipped.txt Executes a query defined by a CommandDefinition and returns a collection of generic type T. ```APIDOC ## Query (IDbConnection, CommandDefinition) ### Description Executes a query defined by a `CommandDefinition` object and returns the results strongly-typed as a collection of generic type `T`. `CommandDefinition` allows for more advanced control over query execution. ### Method static ### Signature Dapper.SqlMapper.Query(this System.Data.IDbConnection cnn, Dapper.CommandDefinition command) ### Parameters - **cnn** (IDbConnection) - The database connection to use. - **command** (CommandDefinition) - A `CommandDefinition` object specifying the query details. ### Returns IEnumerable - An enumerable collection of objects of type `T`. ``` -------------------------------- ### Dapper.DynamicParameters Source: https://github.com/dapperlib/dapper/blob/main/Dapper/PublicAPI.Shipped.txt A collection of parameters for Dapper queries, supporting dynamic addition and retrieval. ```APIDOC ## Dapper.DynamicParameters ### Description A collection of parameters for Dapper queries, supporting dynamic addition and retrieval. ### Constructors - `DynamicParameters()` - `DynamicParameters(object? template)` ### Properties - **ParameterNames** (System.Collections.Generic.IEnumerable) - Gets the names of the parameters. - **RemoveUnused** (bool) - Gets or sets whether to remove unused parameters. ### Methods - `Add(string name, object? value = null, System.Data.DbType? dbType = null, System.Data.ParameterDirection? direction = null, int? size = null, byte? precision = null, byte? scale = null)` - Adds a parameter. - `Add(string name, object value, System.Data.DbType? dbType, System.Data.ParameterDirection? direction, int? size)` - Adds a parameter with specified type, direction, and size. - `AddDynamicParams(object? param)` - Adds parameters from an object. - `AddParameters(System.Data.IDbCommand command, Dapper.SqlMapper.Identity identity)` - Adds parameters to an IDbCommand. - `Get(string name)` - Gets a parameter value by name. - `Output(T target, System.Linq.Expressions.Expression!>! expression, System.Data.DbType? dbType = null, int? size = null)` - Adds an output parameter. ``` -------------------------------- ### QueryAsync(string, Func, object?, IDbTransaction?, bool, string, int?, CommandType?) Source: https://github.com/dapperlib/dapper/blob/main/Dapper/PublicAPI.Shipped.txt Asynchronously executes a query against the database and maps the results to a list of objects of type TReturn, supporting mapping from five types. ```APIDOC ## QueryAsync(string, Func, object?, IDbTransaction?, bool, string, int?, CommandType?) ### Description Asynchronously executes a query against the database and maps the results to a list of objects of type TReturn, supporting mapping from five types. ### Method `Dapper.SqlMapper.QueryAsync(System.Data.IDbConnection, string, System.Func, object?, System.Data.IDbTransaction?, bool, string, int?, System.Data.CommandType?)` ### Parameters - `cnn` (System.Data.IDbConnection) - The database connection. - `sql` (string) - The SQL query to execute. - `map` (System.Func) - A function to map the results to the desired return type. - `param` (object?) - Optional parameters for the query. - `transaction` (System.Data.IDbTransaction?) - Optional transaction. - `buffered` (bool) - Whether to buffer the results. - `splitOn` (string) - The string to split the columns on for mapping. - `commandTimeout` (int?) - Optional command timeout in seconds. - `commandType` (System.Data.CommandType?) - Optional command type. ``` -------------------------------- ### BulkCopy Class Source: https://github.com/dapperlib/dapper/blob/main/Dapper.ProviderTools/PublicAPI.Shipped.txt Provides functionality for efficient bulk data insertion into database tables. ```APIDOC ## BulkCopy Class ### Description Represents a mechanism for performing bulk copy operations. ### Methods #### AddColumnMapping - `AddColumnMapping(int sourceColumn, int destinationColumn)`: Maps a source column index to a destination column index. - `AddColumnMapping(string sourceColumn, string destinationColumn)`: Maps a source column name to a destination column name. #### WriteToServer - `WriteToServer(DataRow[] source)`: Writes an array of data rows to the destination. - `WriteToServer(DataTable source)`: Writes the contents of a data table to the destination. - `WriteToServer(IDataReader source)`: Writes data from an IDataReader to the destination. #### WriteToServerAsync - `WriteToServerAsync(DbDataReader source, CancellationToken cancellationToken = default)`: Asynchronously writes data from a DbDataReader to the destination. - `WriteToServerAsync(DataRow[] source, CancellationToken cancellationToken = default)`: Asynchronously writes an array of data rows to the destination. - `WriteToServerAsync(DataTable source, CancellationToken cancellationToken = default)`: Asynchronously writes the contents of a data table to the destination. ### Properties #### DestinationTableName - `get`: Retrieves the name of the destination table. - `set`: Sets the name of the destination table. #### BatchSize - `get`: Retrieves the batch size for bulk copy operations. - `set`: Sets the batch size for bulk copy operations. #### BulkCopyTimeout - `get`: Retrieves the timeout for bulk copy operations. - `set`: Sets the timeout for bulk copy operations. #### EnableStreaming - `get`: Indicates if streaming is enabled for bulk copy. - `set`: Enables or disables streaming for bulk copy. #### Wrapped - `get`: Retrieves the wrapped underlying bulk copy object. ### Constructors - `BulkCopy()`: Initializes a new instance of the BulkCopy class. ### Static Methods #### Create - `Create(DbConnection connection)`: Creates a new BulkCopy instance for the given database connection. #### TryCreate - `TryCreate(DbConnection connection)`: Attempts to create a BulkCopy instance for the given database connection, returning null if unsuccessful. ### Dispose - `Dispose()`: Releases all resources used by the BulkCopy instance. - `Dispose(bool disposing)`: Releases the unmanaged resources used by the BulkCopy instance and optionally disposes of managed resources. ``` -------------------------------- ### QuerySingle Source: https://github.com/dapperlib/dapper/blob/main/Dapper/PublicAPI.Shipped.txt Retrieves a single record from the query result. Throws an exception if no records or more than one record is found. ```APIDOC ## QuerySingle ### Description Retrieves a single record from the query result. Throws an exception if no records or more than one record is found. ### Method `QuerySingle` (extension method on `System.Data.IDbConnection`) ### Parameters - **cnn** (`System.Data.IDbConnection`) - The database connection. - **sql** (`string`) - The SQL query to execute. - **param** (`object`, optional) - Parameters for the SQL query. - **transaction** (`System.Data.IDbTransaction`, optional) - The transaction to use. - **commandTimeout** (`int`, optional) - The command timeout in seconds. - **commandType** (`System.Data.CommandType`, optional) - The type of the command. ### Overloads - `dynamic QuerySingle(this IDbConnection cnn, string sql, object param = null, IDbTransaction transaction = null, int? commandTimeout = null, CommandType? commandType = null)` - `object QuerySingle(this IDbConnection cnn, Type type, string sql, object param = null, IDbTransaction transaction = null, int? commandTimeout = null, CommandType? commandType = null)` - `T QuerySingle(this IDbConnection cnn, CommandDefinition command)` ``` -------------------------------- ### Querying Typed Objects Source: https://github.com/dapperlib/dapper/blob/main/Readme.md Execute a SQL query and map the results to a list of strongly-typed objects. Ensure the object properties match the SQL column names (case-insensitive by default). ```csharp 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; } } } var guid = Guid.NewGuid(); var dog = connection.Query("select Age = @Age, Id = @Id", new { Age = (int?)null, Id = guid }); Assert.Equal(1,dog.Count()); Assert.Null(dog.First().Age); Assert.Equal(guid, dog.First().Id); ``` -------------------------------- ### QueryAsync(CommandDefinition, Func, string) Source: https://github.com/dapperlib/dapper/blob/main/Dapper/PublicAPI.Shipped.txt Asynchronously executes a query against the database using a CommandDefinition object and maps the results to a list of objects of type TReturn, supporting mapping from five types. ```APIDOC ## QueryAsync(CommandDefinition, Func, string) ### Description Asynchronously executes a query against the database using a CommandDefinition object and maps the results to a list of objects of type TReturn, supporting mapping from five types. ### Method `Dapper.SqlMapper.QueryAsync(System.Data.IDbConnection, Dapper.CommandDefinition, System.Func, string)` ### Parameters - `cnn` (System.Data.IDbConnection) - The database connection. - `command` (Dapper.CommandDefinition) - The command definition containing the query and parameters. - `map` (System.Func) - A function to map the results to the desired return type. - `splitOn` (string) - The string to split the columns on for mapping. ``` -------------------------------- ### QueryAsync(CommandDefinition, Func, string) Source: https://github.com/dapperlib/dapper/blob/main/Dapper/PublicAPI.Shipped.txt Asynchronously executes a query against the database using a CommandDefinition object and maps the results to a list of objects of type TReturn, supporting mapping from two types. ```APIDOC ## QueryAsync(CommandDefinition, Func, string) ### Description Asynchronously executes a query against the database using a CommandDefinition object and maps the results to a list of objects of type TReturn, supporting mapping from two types. ### Method `Dapper.SqlMapper.QueryAsync(System.Data.IDbConnection, Dapper.CommandDefinition, System.Func, string)` ### Parameters - `cnn` (System.Data.IDbConnection) - The database connection. - `command` (Dapper.CommandDefinition) - The command definition containing the query and parameters. - `map` (System.Func) - A function to map the results to the desired return type. - `splitOn` (string) - The string to split the columns on for mapping. ``` -------------------------------- ### QueryAsync(CommandDefinition) Source: https://github.com/dapperlib/dapper/blob/main/Dapper/PublicAPI.Shipped.txt Asynchronously executes a query against the database using a CommandDefinition object and returns a collection of objects of type T. ```APIDOC ## QueryAsync(CommandDefinition) ### Description Asynchronously executes a query against the database using a CommandDefinition object and returns a collection of objects of type T. ### Method `Dapper.SqlMapper.QueryAsync(System.Data.IDbConnection, Dapper.CommandDefinition)` ### Parameters - `cnn` (System.Data.IDbConnection) - The database connection. - `command` (Dapper.CommandDefinition) - The command definition containing the query and parameters. ``` -------------------------------- ### QueryAsync with seven types, SQL string, and custom mapper Source: https://github.com/dapperlib/dapper/blob/main/Dapper/PublicAPI.Shipped.txt Executes a query asynchronously using a SQL string and maps the results to a specified return type with a custom mapping function. This overload is useful for complex queries involving seven types where you prefer to provide the SQL directly. ```APIDOC ## QueryAsync ### Description Executes a query asynchronously using a SQL string and maps the results to a specified return type with a custom mapping function. This overload is useful for complex queries involving seven types where you prefer to provide the SQL directly. ### Method `QueryAsync` ### Parameters - `cnn` (System.Data.IDbConnection) - The database connection. - `sql` (string) - The SQL query to execute. - `map` (System.Func) - A function to map the results. - `param` (object?) - Optional parameters for the SQL query. - `transaction` (System.Data.IDbTransaction?) - Optional transaction to use for the query. - `buffered` (bool) - Whether to buffer the results (default: true). - `splitOn` (string) - The column name to split on when mapping multiple objects (default: "Id"). - `commandTimeout` (int?) - Optional command timeout in seconds. - `commandType` (System.Data.CommandType?) - Optional command type (default: null). ### Return Value System.Threading.Tasks.Task> - A task that represents the asynchronous operation. The task result contains an enumerable collection of the mapped return type. ``` -------------------------------- ### QueryAsync(Type, CommandDefinition) Source: https://github.com/dapperlib/dapper/blob/main/Dapper/PublicAPI.Shipped.txt Asynchronously executes a query against the database using a CommandDefinition object and returns a collection of objects of the specified type. ```APIDOC ## QueryAsync(Type, CommandDefinition) ### Description Asynchronously executes a query against the database using a CommandDefinition object and returns a collection of objects of the specified type. ### Method `Dapper.SqlMapper.QueryAsync(System.Data.IDbConnection, System.Type, Dapper.CommandDefinition)` ### Parameters - `cnn` (System.Data.IDbConnection) - The database connection. - `type` (System.Type) - The type of objects to return. - `command` (Dapper.CommandDefinition) - The command definition containing the query and parameters. ``` -------------------------------- ### QueryAsync with multiple types, SQL string, type array, and custom mapper Source: https://github.com/dapperlib/dapper/blob/main/Dapper/PublicAPI.Shipped.txt Asynchronously executes a query using a SQL string and maps the results to a specified return type. This overload is useful when the return type cannot be inferred and you need to explicitly provide the types involved in the query along with a custom mapping function. ```APIDOC ## QueryAsync ### Description Asynchronously executes a query using a SQL string and maps the results to a specified return type. This overload is useful when the return type cannot be inferred and you need to explicitly provide the types involved in the query along with a custom mapping function. ### Method `QueryAsync` ### Parameters - `cnn` (System.Data.IDbConnection) - The database connection. - `sql` (string) - The SQL query to execute. - `types` (System.Type[]) - An array of types involved in the query. - `map` (System.Func) - A function to map the results. - `param` (object?) - Optional parameters for the SQL query. - `transaction` (System.Data.IDbTransaction?) - Optional transaction to use for the query. - `buffered` (bool) - Whether to buffer the results (default: true). - `splitOn` (string) - The column name to split on when mapping multiple objects (default: "Id"). - `commandTimeout` (int?) - Optional command timeout in seconds. - `commandType` (System.Data.CommandType?) - Optional command type (default: null). ### Return Value System.Threading.Tasks.Task> - A task that represents the asynchronous operation. The task result contains an enumerable collection of the mapped return type. ``` -------------------------------- ### QueryAsync(Type, string, object?) Source: https://github.com/dapperlib/dapper/blob/main/Dapper/PublicAPI.Shipped.txt Asynchronously executes a query against the database and returns a collection of objects of the specified type. ```APIDOC ## QueryAsync(Type, string, object?) ### Description Asynchronously executes a query against the database and returns a collection of objects of the specified type. ### Method `Dapper.SqlMapper.QueryAsync(System.Data.IDbConnection, System.Type, string, object?)` ### Parameters - `cnn` (System.Data.IDbConnection) - The database connection. - `type` (System.Type) - The type of objects to return. - `sql` (string) - The SQL query to execute. - `param` (object?) - Optional parameters for the query. - `transaction` (System.Data.IDbTransaction?) - Optional transaction. - `commandTimeout` (int?) - Optional command timeout in seconds. - `commandType` (System.Data.CommandType?) - Optional command type. ``` -------------------------------- ### QueryAsync with six types, SQL string, and custom mapper Source: https://github.com/dapperlib/dapper/blob/main/Dapper/PublicAPI.Shipped.txt Executes a query asynchronously against the database using a SQL string and maps the results to a specified return type with a custom mapping function. This overload provides flexibility by allowing you to pass the SQL query directly. ```APIDOC ## QueryAsync ### Description Executes a query asynchronously against the database using a SQL string and maps the results to a specified return type with a custom mapping function. This overload provides flexibility by allowing you to pass the SQL query directly. ### Method `QueryAsync` ### Parameters - `cnn` (System.Data.IDbConnection) - The database connection. - `sql` (string) - The SQL query to execute. - `map` (System.Func) - A function to map the results. - `param` (object?) - Optional parameters for the SQL query. - `transaction` (System.Data.IDbTransaction?) - Optional transaction to use for the query. - `buffered` (bool) - Whether to buffer the results (default: true). - `splitOn` (string) - The column name to split on when mapping multiple objects (default: "Id"). - `commandTimeout` (int?) - Optional command timeout in seconds. - `commandType` (System.Data.CommandType?) - Optional command type (default: null). ### Return Value System.Threading.Tasks.Task> - A task that represents the asynchronous operation. The task result contains an enumerable collection of the mapped return type. ``` -------------------------------- ### QueryAsync(string, Func, object?, IDbTransaction?, bool, string, int?, CommandType?) Source: https://github.com/dapperlib/dapper/blob/main/Dapper/PublicAPI.Shipped.txt Asynchronously executes a query against the database and maps the results to a list of objects of type TReturn, supporting mapping from two types. ```APIDOC ## QueryAsync(string, Func, object?, IDbTransaction?, bool, string, int?, CommandType?) ### Description Asynchronously executes a query against the database and maps the results to a list of objects of type TReturn, supporting mapping from two types. ### Method `Dapper.SqlMapper.QueryAsync(System.Data.IDbConnection, string, System.Func, object?, System.Data.IDbTransaction?, bool, string, int?, System.Data.CommandType?)` ### Parameters - `cnn` (System.Data.IDbConnection) - The database connection. - `sql` (string) - The SQL query to execute. - `map` (System.Func) - A function to map the results to the desired return type. - `param` (object?) - Optional parameters for the query. - `transaction` (System.Data.IDbTransaction?) - Optional transaction. - `buffered` (bool) - Whether to buffer the results. - `splitOn` (string) - The string to split the columns on for mapping. - `commandTimeout` (int?) - Optional command timeout in seconds. - `commandType` (System.Data.CommandType?) - Optional command type. ``` -------------------------------- ### QueryFirst with SQL string Source: https://github.com/dapperlib/dapper/blob/main/Dapper/PublicAPI.Shipped.txt Executes a query against the database and returns the first row of the result set as a dynamic object. This is useful for simple queries where you only need the first record and the structure of the result is not strictly defined. ```APIDOC ## QueryFirst ### Description Executes a query against the database and returns the first row of the result set as a dynamic object. This is useful for simple queries where you only need the first record and the structure of the result is not strictly defined. ### Method `QueryFirst` ### Parameters - `cnn` (System.Data.IDbConnection) - The database connection. - `sql` (string) - The SQL query to execute. - `param` (object?) - Optional parameters for the SQL query. - `transaction` (System.Data.IDbTransaction?) - Optional transaction to use for the query. - `commandTimeout` (int?) - Optional command timeout in seconds. - `commandType` (System.Data.CommandType?) - Optional command type (default: null). ### Return Value dynamic - The first row of the result set as a dynamic object. ``` -------------------------------- ### Query (IDbConnection, Type, string) Source: https://github.com/dapperlib/dapper/blob/main/Dapper/PublicAPI.Shipped.txt Executes a SQL query and returns a collection of objects of a specified type. ```APIDOC ## Query (IDbConnection, Type, string) ### Description Executes a SQL query and maps the results to objects of a specified .NET type. This method allows for strongly-typed results directly from a query. ### Method static ### Signature Dapper.SqlMapper.Query(this System.Data.IDbConnection cnn, System.Type type, string sql, object? param = null, System.Data.IDbTransaction? transaction = null, bool buffered = true, int? commandTimeout = null, System.Data.CommandType? commandType = null) ### Parameters - **cnn** (IDbConnection) - The database connection to use. - **type** (Type) - The .NET type to map the results to. - **sql** (string) - The SQL query to execute. - **param** (object?) - Optional - Parameters for the SQL query. - **transaction** (IDbTransaction?) - Optional - The transaction to use for the query. - **buffered** (bool) - Optional - Whether to buffer the results in memory. Defaults to true. - **commandTimeout** (int?) - Optional - The command timeout in seconds. - **commandType** (CommandType?) - Optional - The type of the command (e.g., Text, StoredProcedure). ### Returns IEnumerable - An enumerable collection of objects of the specified type. ``` -------------------------------- ### DynamicParameters for Dynamic SQL Source: https://github.com/dapperlib/dapper/blob/main/Readme.md Build dynamic SQL statements safely and efficiently using the DynamicParameters class. Add parameters with specific DbTypes as needed. ```csharp var sqlPredicates = new List(); var queryParams = new DynamicParameters(); if (boolExpression) { sqlPredicates.Add("column1 = @param1"); queryParams.Add("param1", dynamicValue1, System.Data.DbType.Guid); } else { sqlPredicates.Add("column2 = @param2"); queryParams.Add("param2", dynamicValue2, System.Data.DbType.String); } ``` -------------------------------- ### Insert Parent and Child Records with Foreign Key Source: https://github.com/dapperlib/dapper/blob/main/Dapper.Rainbow/readme.md Inserts a new user, retrieves their ID, and then inserts a new post associated with that user ID. Uses Dapper for the child insert. ```csharp var newUser = new User { Name = "Jane Doe", Email = "jane.doe@example.com" }; var userId = db.Users.Insert(newUser); var newPost = new Post { UserId = userId, Content = "Hello, World!" }; db.Connection.Insert(newPost); // Using Dapper for the child table ``` -------------------------------- ### Dapper.SqlMapper.ExecuteReader Source: https://github.com/dapperlib/dapper/blob/main/Dapper/PublicAPI.Shipped.txt Executes a SQL statement and returns an IDataReader. Supports CommandDefinition and direct SQL string execution. ```APIDOC ## Dapper.SqlMapper.ExecuteReader ### Description Executes a SQL statement and returns an IDataReader. Supports CommandDefinition and direct SQL string execution. ### Method `static System.Data.IDataReader ExecuteReader(this System.Data.IDbConnection cnn, Dapper.CommandDefinition command)` `static System.Data.IDataReader ExecuteReader(this System.Data.IDbConnection cnn, Dapper.CommandDefinition command, System.Data.CommandBehavior commandBehavior)` `static System.Data.IDataReader ExecuteReader(this System.Data.IDbConnection cnn, string sql, object? param = null, System.Data.IDbTransaction? transaction = null, int? commandTimeout = null, System.Data.CommandType? commandType = null)` ### Parameters #### Path Parameters None #### Query Parameters None #### Request Body None ### Request Example None ### Response #### Success Response (200) - **System.Data.IDataReader** - An IDataReader containing the results of the query. ``` -------------------------------- ### QueryAsync with four types, SQL string, and custom mapper Source: https://github.com/dapperlib/dapper/blob/main/Dapper/PublicAPI.Shipped.txt Executes a query asynchronously using a SQL string and maps the results to a specified return type with a custom mapping function. This overload is useful when you need to combine data from four types and prefer to provide the SQL query directly. ```APIDOC ## QueryAsync ### Description Executes a query asynchronously using a SQL string and maps the results to a specified return type with a custom mapping function. This overload is useful when you need to combine data from four types and prefer to provide the SQL query directly. ### Method `QueryAsync` ### Parameters - `cnn` (System.Data.IDbConnection) - The database connection. - `sql` (string) - The SQL query to execute. - `map` (System.Func) - A function to map the results. - `param` (object?) - Optional parameters for the SQL query. - `transaction` (System.Data.IDbTransaction?) - Optional transaction to use for the query. - `buffered` (bool) - Whether to buffer the results (default: true). - `splitOn` (string) - The column name to split on when mapping multiple objects (default: "Id"). - `commandTimeout` (int?) - Optional command timeout in seconds. - `commandType` (System.Data.CommandType?) - Optional command type (default: null). ### Return Value System.Threading.Tasks.Task> - A task that represents the asynchronous operation. The task result contains an enumerable collection of the mapped return type. ``` -------------------------------- ### QueryFirstAsync with CommandDefinition Source: https://github.com/dapperlib/dapper/blob/main/Dapper/PublicAPI.Shipped.txt Asynchronously executes a query defined by a CommandDefinition object and returns the first row of the result set as a dynamic object. This is useful for non-blocking operations where the result structure is not strictly defined. ```APIDOC ## QueryFirstAsync ### Description Asynchronously executes a query defined by a CommandDefinition object and returns the first row of the result set as a dynamic object. This is useful for non-blocking operations where the result structure is not strictly defined. ### Method `QueryFirstAsync` ### Parameters - `cnn` (System.Data.IDbConnection) - The database connection. - `command` (Dapper.CommandDefinition) - An object representing the command to execute. ### Return Value System.Threading.Tasks.Task - A task that represents the asynchronous operation. The task result contains the first row of the result set as a dynamic object. ``` -------------------------------- ### DbString Static Properties Source: https://github.com/dapperlib/dapper/blob/main/Dapper/PublicAPI.Shipped.txt Static properties for configuring DbString behavior. ```APIDOC ## DbString Static Properties ### Description Provides static properties to configure the default behavior of `DbString`. ### Properties - **IsAnsiDefault**: `bool` - Gets or sets a value indicating whether ANSI strings are used by default. ```