NTDLS.SqlServerDapperWrapper 1.2.0

There is a newer version of this package available.
See the version list below for details.
dotnet add package NTDLS.SqlServerDapperWrapper --version 1.2.0                
NuGet\Install-Package NTDLS.SqlServerDapperWrapper -Version 1.2.0                
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.2.0" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add NTDLS.SqlServerDapperWrapper --version 1.2.0                
#r "nuget: NTDLS.SqlServerDapperWrapper, 1.2.0"                
#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.
// Install NTDLS.SqlServerDapperWrapper as a Cake Addin
#addin nuget:?package=NTDLS.SqlServerDapperWrapper&version=1.2.0

// Install NTDLS.SqlServerDapperWrapper as a Cake Tool
#tool nuget:?package=NTDLS.SqlServerDapperWrapper&version=1.2.0                

NTDLS.SqlServerDapperWrapper

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

Provides a simple interface to a SQL Server database with automatic cleanup and stored procedure detection. All managed and wrapped in Dapper (hence the name).

Examples: Keep in mind that you can also use the SqlServerManagedInstance directly if you really want long lived processes.

public static SqlServerManagedFactory MyConnection { get; set; } = new("localhost", "tempdb");

//Each time a statement/query is executed, the NTDLS.SqlServerDapperWrapper will
//  open a connection, execute then close & dispose the connection. 
MyConnection.Execute("DROP TABLE IF EXISTS Test");
//We can execure procedures, t-sql text, or embedded .sql files:

//Test procedure execution.
var procValue = MyConnection.ExecuteScalar<DateTime>("TestProc");
Console.WriteLine($"procValue: {procValue}");

//Test tSQL execution.
var textValue = MyConnection.ExecuteScalar<DateTime>("SELECT GetDate()");
Console.WriteLine($"textValue: {textValue}");

//Test embedded resource script execution.
var sqlValue = MyConnection.ExecuteScalar<DateTime>("TestSqlFile.sql");
Console.WriteLine($"sqlValue: {textValue}");
//Creates a table in two different databases from a script that is an embedded resource in the project.
MyConnection.Execute("CREATE TABLE Test( Id int identity(1,1) not null, [Name] varchar(128) NOT NULL, [Description] varchar(128) NULL)");

//Deletes the data from the table "Test".
MyConnection.Execute("DELETE FROM Test");

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

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

//We can use "Ephemeral" to perform multiple steps on the same connection, such as here where we
//  begin a transaction, insert data and then optionally commit or rollback the transaction.
//  The connection is closed and disposed after Ephemeral() executes.
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;
    }
});


//Just getting the results and writing them to the console.
var results = MyConnection.Query<TestModel>("SELECT * FROM Test");
//Print the results.
foreach (var result in results)
{
    Console.WriteLine($"{result.Id} {result.Name} {result.Description}");
}
}

License

MIT

Product Compatible and additional computed target framework versions.
.NET net6.0 is compatible.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 is compatible.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  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. 
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.2.1 73 12/3/2024
1.2.0 127 7/20/2024
1.1.0 107 7/2/2024
1.0.1 123 7/2/2024

Resolved procedure detection for unparameterized scaler execution.
     Added embedded query script support.