CommonNetFuncs.Sql.SqlServer 5.0.0

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

CommonNetFuncs.Sql.SqlServer

License NuGet Version NuGet Downloads

SQL Server implementation of IDirectQuery from CommonNetFuncs.Sql.Common. Targets net11.0 and netstandard2.1.

Contents

Important behavior

  • Operations accept trusted T-SQL, not a parameter collection. Protect the connection string and configure authentication/TLS appropriately.
  • Parameterize untrusted values with SqlCommand and typed SqlParameter values instead of interpolating sql.
  • Choose dynamic identifiers from a fixed allow-list and quote them correctly.
  • commandTimeoutSeconds defaults to 30; maxRetry defaults to 3 total attempts. Retrying a non-idempotent write can repeat its effect.
  • Table/update exceptions are logged through NLog. After exhaustion, table methods return an empty DataTable and update methods return a default UpdateResult.
  • Mapped operations require T : class, new() and exact public writable property/column names. DBNull maps to the property's default; missing columns and incompatible types can throw DataException.
  • The contract has no transaction argument.

Construction

The constructor accepts an optional connection factory:

using CommonNetFuncs.Sql.SqlServer;
using Microsoft.Data.SqlClient;

DirectQuery defaultFactory = new();
DirectQuery customFactory = new(connectionString =>
    new SqlConnection(connectionString));

Each operation creates, owns, and disposes its provider connection and command. Async-iterator setup is deferred until enumeration starts.

All DirectQuery operations

Every public query method is demonstrated below. The SQL is fixed/trusted:

using System.Data;
using CommonNetFuncs.Sql.Common;
using CommonNetFuncs.Sql.SqlServer;

const string connectionString =
    "Server=sql.example;Database=App;Authentication=Active Directory Default;Encrypt=True;";

DirectQuery queries = new();
CancellationToken cancellationToken = CancellationToken.None;

// GetDataTableAsync
using DataTable asyncTable = await queries.GetDataTableAsync(
    "SELECT Id, Name FROM dbo.Customers WHERE IsActive = 1",
    connectionString, 30, 3, cancellationToken);

// GetDataTable
using DataTable table = queries.GetDataTable(
    "SELECT Id, Name FROM dbo.Customers WHERE IsActive = 1",
    connectionString, 30, 3);

// RunUpdateQueryAsync
UpdateResult asyncResult = await queries.RunUpdateQueryAsync(
    "UPDATE dbo.Customers SET IsActive = 1 WHERE Id = 42",
    connectionString, 30, 1, cancellationToken);

// RunUpdateQuery
UpdateResult result = queries.RunUpdateQuery(
    "UPDATE dbo.Customers SET IsActive = 1 WHERE Id = 42",
    connectionString, 30, 1);

// GetDataStreamAsync<T>
await foreach (Customer row in queries.GetDataStreamAsync<Customer>(
    "SELECT Id, Name FROM dbo.Customers ORDER BY Id",
    connectionString, 30, 3, useCache: true, cancellationToken))
{
    await ProcessAsync(row, cancellationToken);
}

// GetDataStream<T> (see synchronous-stream limitation below)
IEnumerable<Customer> stream = queries.GetDataStream<Customer>(
    "SELECT Id, Name FROM dbo.Customers ORDER BY Id",
    connectionString, 30, 3, useCache: true, cancellationToken);
foreach (Customer row in stream)
{
    Process(row);
}

// GetDataDirectAsync<T>
IEnumerable<Customer> buffered = await queries.GetDataDirectAsync<Customer>(
    "SELECT Id, Name FROM dbo.Customers ORDER BY Id",
    connectionString, 30, 3, useCache: true, cancellationToken);
public sealed class Customer
{
    public int Id { get; set; }
    public string? Name { get; set; }
}

Inspect both update fields:

UpdateResult update = await queries.RunUpdateQueryAsync(sql, connectionString);
if (!update.Success || update.RecordsChanged != 1)
{
    // Handle failure or an unexpected affected-row count.
}

An empty result table can mean either zero rows or a logged error.

Streaming and ownership

GetDataStreamAsync<T> retains its connection/command until the async sequence finishes or is disposed. Consume it completely or dispose the enumerator promptly.

The current streaming retry helper does not surround exceptions raised during MoveNextAsync, so a runtime query/mapping failure may not be retried despite maxRetry.

GetDataStream<T> currently returns a lazy sequence after its outer connection/command scope is disposed. Do not rely on this provider overload in the current release. Prefer GetDataStreamAsync<T> or use CommonNetFuncs.Sql.Common.DirectQuery.GetDataStream<T> with a caller-owned SqlConnection.

GetDataDirectAsync<T> buffers all mapped rows and throws DataException after all attempts fail.

Parameterized SQL Server commands

Use lower-level provider ADO.NET for values:

using Microsoft.Data.SqlClient;

await using SqlConnection connection = new(connectionString);
await connection.OpenAsync(cancellationToken);
await using SqlCommand command = new(
    "SELECT Id, Name FROM dbo.Customers WHERE TenantId = @tenantId",
    connection);
command.Parameters.Add(new SqlParameter("@tenantId", tenantId));

await using SqlDataReader reader =
    await command.ExecuteReaderAsync(cancellationToken);
while (await reader.ReadAsync(cancellationToken))
{
    Console.WriteLine($"{reader.GetInt32(0)} {reader.GetString(1)}");
}

Transactions

The wrapper methods cannot participate in an existing transaction. Use SqlTransaction directly for atomic work:

await using SqlConnection connection = new(connectionString);
await connection.OpenAsync(cancellationToken);
await using SqlTransaction transaction =
    (SqlTransaction)await connection.BeginTransactionAsync(cancellationToken);

try
{
    await using SqlCommand debit = connection.CreateCommand();
    debit.Transaction = transaction;
    debit.CommandText =
        "UPDATE dbo.Accounts SET Balance = Balance - @amount WHERE Id = @id";
    debit.Parameters.Add(new SqlParameter("@amount", amount));
    debit.Parameters.Add(new SqlParameter("@id", sourceId));
    await debit.ExecuteNonQueryAsync(cancellationToken);

    await transaction.CommitAsync(cancellationToken);
}
catch
{
    await transaction.RollbackAsync(cancellationToken);
    throw;
}

Diagnostic T-SQL rendering

On net11.0, the package exposes CommonNetFuncs.Sql.SqlServer.Debug.RenderCommandAsScript(DbCommand). This API is excluded from the netstandard2.1 asset by #if !NETSTANDARD2_1.

using System.Data;
using Microsoft.Data.SqlClient;
using CommonNetFuncs.Sql.SqlServer;

using SqlCommand command = new(
    "SELECT Id, Name FROM dbo.Customers WHERE Id = @id");
command.Parameters.Add(new SqlParameter("@id", SqlDbType.Int)
{
    Value = 42
});

string diagnosticScript = Debug.RenderCommandAsScript(command);

It emits DECLARE statements and the command text for tools such as SSMS. It includes actual parameter values and is diagnostics only, not a safe substitute for runtime parameterization or a log suitable for untrusted access.

Installation

dotnet add package CommonNetFuncs.Sql.SqlServer

License

MIT. See LICENSE.

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.  net11.0 is compatible. 
.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
5.0.0 62 11/23/2026
4.5.7 67 11/20/2026
4.5.6 92 11/11/2026
4.5.5 117 10/27/2026
4.5.4 111 10/17/2026
4.5.3 105 10/15/2026
4.5.2 108 10/10/2026
4.5.0 104 10/9/2026
4.4.4 128 10/1/2026
4.4.3 118 9/18/2026
4.4.2 116 9/15/2026
4.4.0 119 8/31/2026
4.3.2 130 8/24/2026
4.3.1 125 8/11/2026
4.3.0 134 8/3/2026
4.1.14 134 8/3/2026
4.1.12 138 7/31/2026
4.1.11 147 7/9/2026
4.1.8 143 7/7/2026
4.1.0 156 6/5/2026
Loading failed