/Dapper.Scripts

Extension of Dapper which supports binding a sql-script collection to a sql-connection.

Primary LanguageC#Apache License 2.0Apache-2.0

Dapper Scripts

Extension of Dapper which supports binding a collection of SQL scripts to a SqlConnection.

Avoid embedding SQL text strings into your code, and leverage syntax hightlighting, by storing your SQL operations in external *.sql files. Dapper scripts binds a collection of key-based sql strings to a connection factory, allowing you to keep your database code clean. All extensions match the original dapper commands, but using a *Script suffix.

Setup

static class Sql
{
    public static SqlScriptConnectionFactory SampleDatabase {get;}
    
    static Sql()
    {
        var scripts = new SqlScriptCollection();
        scripts.Add.FromFile("test-query", "query.sql");

        DatabaseName = new SqlScriptConnectionFactory(scripts);
        DatabaseName.ConnectionString = "Server: ...;";
    }
}

Note: For best results, it is recommended to use Embedded Content as the Content Type for your sql files. This will allow them to be stored within your assembly file after compiling.

Usage

using (var connection = Sql.SampleDatabase.Connect()) {
    return connection.QueryScript("test-query", new {arg = value});
}

For more examples, visit the WiKi.