NTDLS.SqlServerDapperWrapper 1.5.6

dotnet add package NTDLS.SqlServerDapperWrapper --version 1.5.6
                    
NuGet\Install-Package NTDLS.SqlServerDapperWrapper -Version 1.5.6
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="NTDLS.SqlServerDapperWrapper" Version="1.5.6" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="NTDLS.SqlServerDapperWrapper" Version="1.5.6" />
                    
Directory.Packages.props
<PackageReference Include="NTDLS.SqlServerDapperWrapper" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add NTDLS.SqlServerDapperWrapper --version 1.5.6
                    
#r "nuget: NTDLS.SqlServerDapperWrapper, 1.5.6"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package NTDLS.SqlServerDapperWrapper@1.5.6
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=NTDLS.SqlServerDapperWrapper&version=1.5.6
                    
Install as a Cake Addin
#tool nuget:?package=NTDLS.SqlServerDapperWrapper&version=1.5.6
                    
Install as a Cake Tool

NTDLS.SqlServerDapperWrapper

NTDLS.SqlServerDapperWrapper is a lightweight C# NuGet package that provides a simple, managed interface to SQL Server on top of Dapper. Every call you make opens a connection, executes your statement, and closes and disposes the connection for you — no connection bookkeeping required. It also transparently detects when you pass a stored procedure name (and runs it as CommandType.StoredProcedure), accepts raw t-SQL text, or loads .sql scripts embedded in your project as resources. All of this is available in both synchronous and asynchronous flavors.

📦 Be sure to check out the NuGet package: https://www.nuget.org/packages/NTDLS.SqlServerDapperWrapper

When to use it

You'll want this package if any of the following sound familiar:

  • You're tired of boilerplate. Opening, closing, and disposing SqlConnections around every Dapper call is repetitive and easy to get wrong. This wrapper handles the lifecycle for you so you can focus on the query.
  • You need short-lived, fire-and-forget database access. Scripts, maintenance jobs, CLI tools, tests, and small utilities often just need "run this one statement" without managing a long-lived connection pool or connection object.
  • You mix stored procedures, ad-hoc t-SQL, and script files. Instead of choosing between CommandType.StoredProcedure and CommandType.Text, just pass the string — a bare procedure name like TestProc is detected automatically, while anything else is treated as t-SQL text.
  • You keep SQL in embedded resource files. Pass the name of an embedded .sql file (e.g. "Scripts/CreateTables.sql") and it will be loaded and executed for you.
  • You want transactions across multiple statements without holding a connection open yourself. The Ephemeral(...) / EphemeralAsync(...) methods give you a single connection for the duration of a delegate, where you can begin a transaction, run several statements, and commit or roll back — all cleaned up automatically afterwards.

If you do want a long-lived process with a persistent connection, you can use the underlying SqlServerManagedInstance class directly instead of going through the factory.

Usage examples

Create a factory

using NTDLS.SqlServerDapperWrapper;

// Build a factory from a server and database name (uses Windows integrated security).
public static SqlServerManagedFactory MyConnection { get; set; } = new("localhost", "tempdb");

Other constructors are available: from a full connection string, from IConfiguration (new(configuration, "MyConnectionString")), or with username/password and an optional command timeout.

Execute a statement

// Each call opens a connection, executes the statement, then closes & disposes the connection.
MyConnection.Execute("DROP TABLE IF EXISTS Test");

// Create a table using inline t-SQL text.
MyConnection.Execute("CREATE TABLE Test (Id int identity(1,1) NOT NULL, [Name] varchar(128) NOT NULL, [Description] varchar(128) NULL)");

Stored procedures, t-SQL, and embedded scripts

The same method accepts three kinds of input, detected automatically:

// 1. A stored procedure name is detected and executed as CommandType.StoredProcedure.
var procValue = MyConnection.ExecuteScalar<DateTime>("TestProc");

// 2. Raw t-SQL text is executed as CommandType.Text.
var textValue = MyConnection.ExecuteScalar<DateTime>("SELECT GetDate()");

// 3. An embedded .sql resource file is loaded by name and executed.
var sqlValue = MyConnection.ExecuteScalar<DateTime>("TestSqlFile.sql");

Parameters

// Insert records using an inline statement and parameters.
for (int i = 0; i < 100; i++)
{
    var param = new
    {
        Name = $"Name #{i}",
        Description = Guid.NewGuid().ToString()
    };

    // Anonymous types work directly as Dapper parameters.
    MyConnection.Execute("INSERT INTO Test (Name, Description) VALUES (@Name, @Description)", param);
}

Multiple statements on one connection (transactions)

// Ephemeral() gives the delegate a single open connection for its lifetime,
// then closes & disposes it when the delegate returns.
MyConnection.Ephemeral(o =>
{
    using var tx = o.BeginTransaction();

    try
    {
        for (int i = 0; i < 100; i++)
        {
            var param = new
            {
                Name = $"Name #{i}",
                Description = Guid.NewGuid().ToString()
            };

            o.Execute("INSERT INTO Test (Name, Description) VALUES (@Name, @Description)", param);
        }

        tx.Commit();
    }
    catch
    {
        tx.Rollback();
        throw;
    }
});

There are async equivalents (EphemeralAsync) and generic overloads that return a value from the delegate.

Query results

// Map query results to a POCO.
var results = MyConnection.Query<TestModel>("SELECT * FROM Test");

foreach (var result in results)
{
    Console.WriteLine($"{result.Id} {result.Name} {result.Description}");
}

Full Dapper-style coverage is provided: Query, QueryFirst, QueryFirstOrDefault, QuerySingle, QuerySingleOrDefault, ExecuteScalar, and Execute — each with parameter and optional commandTimeout overloads, plus matching ...Async versions.

License

MIT

Product Compatible and additional computed target framework versions.
.NET net8.0 is compatible.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed.  net9.0 is compatible.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed.  net10.0 is compatible.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
1.5.6 56 10/21/2026
1.5.5 124 9/5/2026
1.5.4 122 8/18/2026
1.5.3 149 6/30/2026
1.5.2 144 6/30/2026
1.5.1 139 6/8/2026
1.5.0 154 2/20/2026
1.4.2 731 12/3/2025
1.4.1 354 11/13/2025
1.4.0 343 11/12/2025
1.3.3 383 7/26/2025
1.3.2 388 7/26/2025
1.3.1 383 7/26/2025
1.3.0 383 7/26/2025
1.2.5 200 5/2/2025
1.2.4 254 3/28/2025
1.2.3 224 3/28/2025
1.2.2 245 12/31/2024
1.2.1 211 12/3/2024
1.2.0 258 7/20/2024
Loading failed

Update dependencies and override vulnerable cryptography package