CommonNetFuncs.Sql.Odbc
5.0.0
dotnet add package CommonNetFuncs.Sql.Odbc --version 5.0.0
NuGet\Install-Package CommonNetFuncs.Sql.Odbc -Version 5.0.0
<PackageReference Include="CommonNetFuncs.Sql.Odbc" Version="5.0.0" />
<PackageVersion Include="CommonNetFuncs.Sql.Odbc" Version="5.0.0" />
<PackageReference Include="CommonNetFuncs.Sql.Odbc" />
paket add CommonNetFuncs.Sql.Odbc --version 5.0.0
#r "nuget: CommonNetFuncs.Sql.Odbc, 5.0.0"
#:package CommonNetFuncs.Sql.Odbc@5.0.0
#addin nuget:?package=CommonNetFuncs.Sql.Odbc&version=5.0.0
#tool nuget:?package=CommonNetFuncs.Sql.Odbc&version=5.0.0
CommonNetFuncs.Sql.Odbc
ODBC implementation of IDirectQuery from CommonNetFuncs.Sql.Common. Targets net11.0 and netstandard2.1.
Contents
Prerequisites
Install an ODBC driver for the target database and process architecture, and configure/protect the connection string. This package does not install drivers or data sources. Driver SQL syntax, parameter markers, affected-row behavior, and transaction support remain provider-specific.
Important behavior
- Every operation accepts raw SQL text, not a parameter collection. The provider wrapper creates only a connection and command.
- Parameterize untrusted values with
OdbcCommandand typedOdbcParameterinstances in a lower-level call. ODBC commonly uses positional?markers; follow the selected driver's convention. - Dynamic identifiers must come from a fixed allow-list.
commandTimeoutSecondsdefaults to30.maxRetryis the total number of attempts; its default3is not three additional retries. Avoid retrying non-idempotent writes.- Table and update failures are logged through NLog and converted to an empty
DataTableor failedUpdateResult. GetDataDirectAsyncbuffers mapped rows and throwsDataExceptionafter exhausted retries.- The contract has no transaction argument.
Mapped operations require T : class, new(). Public writable properties are mapped from exact column names. Missing/incompatible columns and values can throw DataException; DBNull maps to the property's default value. useCache controls the shared compiled-mapper cache and defaults to true.
Construction
The default constructor creates OdbcConnection instances:
using CommonNetFuncs.Sql.Odbc;
DirectQuery queries = new();
The optional connection factory is a public constructor overload:
using System.Data.Odbc;
using CommonNetFuncs.Sql.Common;
IDirectQuery queries = new DirectQuery(connectionString =>
new OdbcConnection(connectionString));
The factory receives the exact connStr passed to each operation. It runs when the operation executes; an async-iterator body does not execute until enumeration starts.
Complete DirectQuery examples
The following calls clearly demonstrate all seven public operations. SQL is fixed/trusted; for user values see Parameterized ODBC SQL.
using System.Data;
using CommonNetFuncs.Sql.Common;
using CommonNetFuncs.Sql.Odbc;
const string connectionString =
"Driver={ODBC Driver 18 for SQL Server};Server=sql.example;Database=App;Trusted_Connection=Yes;Encrypt=Yes;";
DirectQuery queries = new();
CancellationToken cancellationToken = CancellationToken.None;
// GetDataTableAsync overload
using DataTable asyncTable = await queries.GetDataTableAsync(
"SELECT Id, Name FROM Customers WHERE IsActive = 1",
connectionString,
commandTimeoutSeconds: 30,
maxRetry: 3,
cancellationToken);
// GetDataTable
using DataTable table = queries.GetDataTable(
"SELECT Id, Name FROM Customers WHERE IsActive = 1",
connectionString,
commandTimeoutSeconds: 30,
maxRetry: 3);
// RunUpdateQueryAsync
UpdateResult asyncUpdate = await queries.RunUpdateQueryAsync(
"UPDATE Customers SET IsActive = 1 WHERE Id = 42",
connectionString,
commandTimeoutSeconds: 30,
maxRetry: 1,
cancellationToken);
// RunUpdateQuery
UpdateResult update = queries.RunUpdateQuery(
"UPDATE Customers SET IsActive = 1 WHERE Id = 42",
connectionString,
commandTimeoutSeconds: 30,
maxRetry: 1);
// GetDataStreamAsync<T>
await foreach (Customer row in queries.GetDataStreamAsync<Customer>(
"SELECT Id, Name FROM Customers ORDER BY Id",
connectionString,
commandTimeoutSeconds: 30,
maxRetry: 3,
useCache: true,
cancellationToken))
{
await ProcessAsync(row, cancellationToken);
}
// GetDataStream<T> (see the synchronous-stream limitation below)
IEnumerable<Customer> stream = queries.GetDataStream<Customer>(
"SELECT Id, Name FROM Customers ORDER BY Id",
connectionString,
commandTimeoutSeconds: 30,
maxRetry: 3,
useCache: true,
cancellationToken);
foreach (Customer row in stream)
{
Process(row);
}
// GetDataDirectAsync<T>
IEnumerable<Customer> buffered = await queries.GetDataDirectAsync<Customer>(
"SELECT Id, Name FROM Customers ORDER BY Id",
connectionString,
commandTimeoutSeconds: 30,
maxRetry: 3,
useCache: true,
cancellationToken);
Customer is an ordinary mutable class:
public sealed class Customer
{
public int Id { get; set; }
public string? Name { get; set; }
}
Result and failure semantics
For updates, always inspect both fields:
UpdateResult result = await queries.RunUpdateQueryAsync(sql, connectionString);
if (!result.Success || result.RecordsChanged != 1)
{
// The update failed, changed no rows, or changed more than expected.
}
An empty table from GetDataTable* is ambiguous: it may contain no matching rows or represent an exhausted, logged failure.
Streaming lifetime and retry limitation
GetDataStreamAsync<T> keeps its provider connection and command alive until the async enumeration completes or is disposed; fully consume or promptly dispose it. The current maxRetry wrapper is created before enumeration and does not surround failures that occur on MoveNextAsync, so a runtime execution failure may not be retried.
The provider GetDataStream<T> currently returns a lazy enumerable after disposing its outer connection/command scope. Do not rely on deferred enumeration of this provider overload in the current release. Prefer GetDataStreamAsync<T>, or use CommonNetFuncs.Sql.Common.DirectQuery.GetDataStream<T> with a caller-owned OdbcConnection.
Parameterized ODBC SQL
Use provider ADO.NET for values rather than concatenation:
using System.Data.Odbc;
await using OdbcConnection connection = new(connectionString);
await connection.OpenAsync(cancellationToken);
await using OdbcCommand command = connection.CreateCommand();
command.CommandText = "SELECT Id, Name FROM Customers WHERE Id = ?";
command.Parameters.Add(new OdbcParameter { Value = 42 });
await using OdbcDataReader reader =
await command.ExecuteReaderAsync(cancellationToken);
while (await reader.ReadAsync(cancellationToken))
{
Console.WriteLine($"{reader.GetInt32(0)} {reader.GetString(1)}");
}
Transactions
Multi-operation atomicity requires an explicit provider transaction:
await using OdbcConnection connection = new(connectionString);
await connection.OpenAsync(cancellationToken);
await using OdbcTransaction transaction =
(OdbcTransaction)await connection.BeginTransactionAsync(cancellationToken);
await using OdbcCommand command = connection.CreateCommand();
command.Transaction = transaction;
try
{
command.CommandText = "UPDATE Accounts SET Balance = Balance - 10 WHERE Id = ?";
command.Parameters.Add(new OdbcParameter { Value = 1 });
await command.ExecuteNonQueryAsync(cancellationToken);
await transaction.CommitAsync(cancellationToken);
}
catch
{
await transaction.RollbackAsync(cancellationToken);
throw;
}
DirectQuery does not participate in this transaction because it cannot accept connection, command, or transaction; use the provider command directly for the transaction's commands.
Installation
dotnet add package CommonNetFuncs.Sql.Odbc
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)
- NLog.Extensions.Logging (>= 6.3.0)
- System.Data.Odbc (>= 10.0.15)
-
net11.0
- CommonNetFuncs.Sql.Common (>= 5.0.0)
- NLog.Extensions.Logging (>= 6.3.0)
- System.Data.Odbc (>= 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 | 80 | 11/23/2026 |
| 4.5.7 | 110 | 11/11/2026 |
| 4.5.6 | 152 | 10/27/2026 |
| 4.5.5 | 122 | 10/17/2026 |
| 4.5.4 | 116 | 10/15/2026 |
| 4.5.3 | 128 | 10/14/2026 |
| 4.5.2 | 126 | 10/10/2026 |
| 4.5.0 | 112 | 10/9/2026 |
| 4.4.2 | 159 | 9/18/2026 |
| 4.4.1 | 122 | 9/15/2026 |
| 4.4.0 | 135 | 8/31/2026 |
| 4.3.3 | 136 | 8/24/2026 |
| 4.3.2 | 149 | 8/14/2026 |
| 4.3.1 | 143 | 8/11/2026 |
| 4.3.0 | 150 | 8/3/2026 |
| 4.1.15 | 140 | 8/3/2026 |
| 4.1.13 | 136 | 7/31/2026 |
| 4.1.12 | 140 | 7/16/2026 |
| 4.1.11 | 144 | 7/9/2026 |
| 4.1.3 | 152 | 6/9/2026 |