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
<PackageReference Include="CommonNetFuncs.Sql.SqlServer" Version="5.0.0" />
<PackageVersion Include="CommonNetFuncs.Sql.SqlServer" Version="5.0.0" />
<PackageReference Include="CommonNetFuncs.Sql.SqlServer" />
paket add CommonNetFuncs.Sql.SqlServer --version 5.0.0
#r "nuget: CommonNetFuncs.Sql.SqlServer, 5.0.0"
#:package CommonNetFuncs.Sql.SqlServer@5.0.0
#addin nuget:?package=CommonNetFuncs.Sql.SqlServer&version=5.0.0
#tool nuget:?package=CommonNetFuncs.Sql.SqlServer&version=5.0.0
CommonNetFuncs.Sql.SqlServer
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
SqlCommandand typedSqlParametervalues instead of interpolatingsql. - Choose dynamic identifiers from a fixed allow-list and quote them correctly.
commandTimeoutSecondsdefaults to30;maxRetrydefaults to3total attempts. Retrying a non-idempotent write can repeat its effect.- Table/update exceptions are logged through NLog. After exhaustion, table methods return an empty
DataTableand update methods return a defaultUpdateResult. - Mapped operations require
T : class, new()and exact public writable property/column names.DBNullmaps to the property's default; missing columns and incompatible types can throwDataException. - 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 | Versions 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. |
-
.NETStandard 2.1
- CommonNetFuncs.Sql.Common (>= 5.0.0)
- Microsoft.Data.SqlClient (>= 7.1.2)
- System.Security.Cryptography.Pkcs (>= 8.0.2)
-
net11.0
- CommonNetFuncs.Sql.Common (>= 5.0.0)
- Microsoft.Data.SqlClient (>= 7.1.2)
- System.Security.Cryptography.Pkcs (>= 11.0.0)
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 |