ConstantSQL 0.1.1

There is a newer version of this package available.
See the version list below for details.
dotnet add package ConstantSQL --version 0.1.1
                    
NuGet\Install-Package ConstantSQL -Version 0.1.1
                    
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="ConstantSQL" Version="0.1.1">
  <PrivateAssets>all</PrivateAssets>
  <IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets>
</PackageReference>
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="ConstantSQL" Version="0.1.1" />
                    
Directory.Packages.props
<PackageReference Include="ConstantSQL">
  <PrivateAssets>all</PrivateAssets>
  <IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets>
</PackageReference>
                    
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 ConstantSQL --version 0.1.1
                    
#r "nuget: ConstantSQL, 0.1.1"
                    
#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 ConstantSQL@0.1.1
                    
#: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=ConstantSQL&version=0.1.1
                    
Install as a Cake Addin
#tool nuget:?package=ConstantSQL&version=0.1.1
                    
Install as a Cake Tool

ConstantSQL

A Roslyn source generator that reads SQL files at compile time and generates a C# class with const string values for each SQL file content. This gives you the best of both worlds, direct C# constants but a SQL file with SQL syntax support with formatting and suggestions applicable to SQL without requiring special IDE integration.

Installation

dotnet add package ConstantSQL

Usage

ConstantSQL supports two ways to organize your SQL files:

Single-Statement Files

One SQL query per file - the constant is named after the file:

GetUsers.sql:

SELECT * FROM Users WHERE Active = 1

Generated code:

public const string GetUsers = @"SELECT * FROM Users WHERE Active = 1";

Multi-Statement Files

Multiple named SQL queries in a single file using -- @name: markers:

UserQueries.sql:

-- @name: GetActiveUsers
SELECT Id, Name, Email 
FROM Users 
WHERE Active = 1;

-- @name: GetUserById
SELECT Id, Name, Email, CreatedDate
FROM Users 
WHERE Id = @UserId;

-- @name: UpdateUserEmail
UPDATE Users 
SET Email = @Email, ModifiedDate = GETUTCDATE()
WHERE Id = @UserId;

Generated code:

public const string GetActiveUsers = @"SELECT Id, Name, Email 
FROM Users 
WHERE Active = 1;";

public const string GetUserById = @"SELECT Id, Name, Email, CreatedDate
FROM Users 
WHERE Id = @UserId;";

public const string UpdateUserEmail = @"UPDATE Users 
SET Email = @Email, ModifiedDate = GETUTCDATE()
WHERE Id = @UserId;";

Using the Generated Constants

using ConstantSQL.SqlFiles;

// From single-statement file
var query1 = Queries.GetUsers;

// From multi-statement file
var query2 = Queries.GetActiveUsers;
var query3 = Queries.UpdateUserEmail;

Configuration

By default, the source generator looks for all *.sql files in your project:

<ItemGroup>
  
  <SqlFiles Include="**\*.sql" />
</ItemGroup>

Features

  • ✅ Compile-time SQL file processing
  • ✅ Strongly-typed access to SQL queries
  • ✅ IntelliSense support
  • ✅ No runtime file I/O
  • ✅ Automatic handling of special characters and escaping
  • ✅ Support for files with duplicate names (with automatic suffixing)
  • ✅ Single-statement files (one query per file)
  • ✅ Multi-statement files (multiple named queries per file using -- @name: markers)

Example

Given a SQL file GetActiveUsers.sql:

SELECT Id, Name, Email 
FROM Users 
WHERE Active = 1
ORDER BY Name

The generator creates:

namespace ConstantSQL.SqlFiles
{
    public static class Queries
    {
        public const string GetActiveUsers = @"SELECT Id, Name, Email 
FROM Users 
WHERE Active = 1
ORDER BY Name";
    }
}

License

Apache 2.0

There are no supported framework assets in this package.

Learn more about Target Frameworks and .NET Standard.

  • .NETStandard 2.0

    • No dependencies.

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
0.1.2 484 7/23/2025
0.1.1 482 7/23/2025
0.1.0 486 7/22/2025