Controtex.SqlUtility2 2.0.1

dotnet add package Controtex.SqlUtility2 --version 2.0.1
                    
NuGet\Install-Package Controtex.SqlUtility2 -Version 2.0.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="Controtex.SqlUtility2" Version="2.0.1" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Controtex.SqlUtility2" Version="2.0.1" />
                    
Directory.Packages.props
<PackageReference Include="Controtex.SqlUtility2" />
                    
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 Controtex.SqlUtility2 --version 2.0.1
                    
#r "nuget: Controtex.SqlUtility2, 2.0.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 Controtex.SqlUtility2@2.0.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=Controtex.SqlUtility2&version=2.0.1
                    
Install as a Cake Addin
#tool nuget:?package=Controtex.SqlUtility2&version=2.0.1
                    
Install as a Cake Tool

Controtex.SqlUtility2

Controtex.SqlUtility2 is a C# library that simplifies querying SQL Server databases and always returns results in structured JSON format.

It supports executing:

  • Stored procedures (with or without parameters)
  • SQL queries returning tables or scalar values
  • Arbitrary SQL commands (e.g. INSERT/UPDATE/DELETE)
  • Automatic conversion of all results to JArray / JObject (via Newtonsoft.Json)

๐Ÿ”ง Features

  • โœ… JSON output for everything: JArray / JObject
  • โœ… Stored procedure support, including multiple result sets
  • โœ… Auto-discovery of procedure parameter names via sys.parameters
  • โœ… Parameter binding by name or position
  • โœ… Unified API for querying and executing
  • โœ… Lightweight, dependency-free (aside from Newtonsoft.Json and Microsoft.Data.SqlClient)

๐Ÿ“ฆ Installation

dotnet add package Controtex.SqlUtility2

๐Ÿš€ Basic Usage

var db = new SqlUtility("your-connection-string");

// Get rows from a table
JArray users = db.Table("Users");

// Run a non-query SQL command
int rows = db.ExecuteCommand("DELETE FROM Logs WHERE Level = @level", "level", "Debug");

// Get a single scalar value
object count = db.ExecuteScalar("SELECT COUNT(*) FROM Users");

๐Ÿ”„ Calling Stored Procedures

๐Ÿงช Example 1: No parameters

JArray? result = db.Call("GetAllUsers");

๐Ÿงช Example 2: One parameter

JArray? result = db.Call("GetUsersByRole", "role", "Admin");

Equivalent to:

EXEC GetUsersByRole @role = 'Admin'

๐Ÿงช Example 3: Multiple parameters

var result = db.Call("GetUsersByStatus",
    new[] { "status", "country" },
    "Active", "PL");

๐Ÿงช Example 4: All result sets (multi-table)

JObject results = db.CallAllResults("GetDashboardStats", new object[] { 2024 });

// Access results by name
JArray stats = (JArray)results["Stats"];
JArray errors = (JArray)results["Errors"];

๐Ÿงช Example 5: Get single object

JObject? user = db.CallOne("GetUserById", 123);
string? name = user?["Name"]?.Value<string>();

๐Ÿ“Š Table Queries

Basic table data

JArray? orders = db.Table("Orders", "UserId = @id", "id", 42);

One row from a table

JObject? latestOrder = db.TableOne("Orders", "UserId = @id", "Date DESC", "id", 42);

Top N results with order

JArray? topItems = db.TableTop("Products", 5, "Category = @cat", "Price DESC", new[] { "cat" }, "Books");

๐Ÿ” Execute SQL Commands

Non-query

int affected = db.ExecuteCommand("UPDATE Users SET Active = 0 WHERE LastLogin < @date", "date", DateTime.Now.AddMonths(-6));

Scalar result

object result = db.ExecuteScalar("SELECT COUNT(*) FROM Users WHERE Active = 1");

๐Ÿ“„ License

MIT License


โœ‰๏ธ Support

Issues, suggestions, or feedback? Please post them on the NuGet package page.

Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 was computed.  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 was computed.  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 was computed.  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 was computed.  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 was computed.  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. 
.NET Core netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.1 is compatible. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen60 was computed. 
Xamarin.iOS xamarinios was computed. 
Xamarin.Mac xamarinmac was computed. 
Xamarin.TVOS xamarintvos was computed. 
Xamarin.WatchOS xamarinwatchos 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
2.0.1 184 7/18/2025
2.0.0 200 7/18/2025
1.3.2 577 9/9/2021
1.3.1 531 9/9/2021
1.3.0 528 9/9/2021
1.2.1 462 9/3/2021
1.2.0 499 9/2/2021
1.1.3 488 8/27/2021
1.1.1 485 8/27/2021
1.1.0 494 8/26/2021
1.0.0 492 8/25/2021

Added readme