NTDLS.SqlManagedConnectivity Usage
A managed wrapper around native SQL Server connections with easy field and value enumeration.
NTDLS.SqlManagedConnectivity
What it is. A wrapper around a native-style SQL Server connection that handles cleanup for you and makes it easy to enumerate the fields (with their data types) and the rows/values they contain, converting values to the C# type you want. It sits between raw SqlConnection/SqlDataReader code and a full ORM.
When to reach for it. You are writing classic reader-based T-SQL access - dynamic schemas, ad-hoc reporting, tools that inspect columns at runtime - and want typed value extraction without boilerplate. Skip it if you prefer POCO mapping and parameterized query helpers; the sibling package NTDLS.SqlServerDapperWrapper (wrapped in Dapper) is the better fit there.
Getting started
Open a connection by server and database name, run a query, then walk the result set:
using (var connection = new SqlManagedConnection(".", "Dummyload"))
{
using (var reader = connection.ExecuteQuery(
"SELECT * FROM Test WHERE Account <> @Account", new { Account = 4104 }))
{
// Inspect every column and its type:
foreach (var field in reader.Fields)
{
Console.WriteLine($"Field: '{field.Name}', Data Type: '{field.DataTypeName}'");
}
// Walk every row and value:
foreach (var row in reader)
{
foreach (var value in row)
{
Console.WriteLine($"{value.Field.Name} -> '{value.As<string>()?.Trim()}'");
}
// Or pull specific columns straight into typed variables:
double percentTaxable = row.Value<double>("PercentTaxable");
decimal originalAmount = row.Value<decimal>("OriginalAmount");
int amountAsInt = row.Value<int>("OriginalAmount");
}
}
}
Both the connection and the reader dispose themselves at the end of their scopes, so no explicit close calls are needed. Named parameters work the way you would expect from @Parameter placeholders in your T-SQL.
NuGet: NTDLS.SqlManagedConnectivity