ErikEJ.SqlClient.Extensions
1.0.0-preview1
Prefix Reserved
See the version list below for details.
Requires NuGet 5.0 or higher.
dotnet add package ErikEJ.SqlClient.Extensions --version 1.0.0-preview1
NuGet\Install-Package ErikEJ.SqlClient.Extensions -Version 1.0.0-preview1
<PackageReference Include="ErikEJ.SqlClient.Extensions" Version="1.0.0-preview1" />
paket add ErikEJ.SqlClient.Extensions --version 1.0.0-preview1
#r "nuget: ErikEJ.SqlClient.Extensions, 1.0.0-preview1"
// Install ErikEJ.SqlClient.Extensions as a Cake Addin #addin nuget:?package=ErikEJ.SqlClient.Extensions&version=1.0.0-preview1&prerelease // Install ErikEJ.SqlClient.Extensions as a Cake Tool #tool nuget:?package=ErikEJ.SqlClient.Extensions&version=1.0.0-preview1&prerelease
ErikEJ.SqlClient.Extensions
Adds support for .NET Dependency Injection and Logging for Microsoft.Data.SqlClient.
Installation
Install the latest package from NuGet.
Getting started
Microsoft.Data.SqlClient
is the open source .NET data provider for Microsoft SQL Server. It allows you to connect and interact with SQL Server and Azure SQL Database using .NET.
This package helps set up SqlClient in applications using dependency injection, notably ASP.NET and Worker Service applications. It allows easy configuration of your database connections and registers the appropriate services in your DI container. It also enables you to log events from Microsoft.Data.SqlClient using standard .NET logging (ILogger).
For example, if using the ASP.NET minimal web API, simply use the following to register Microsoft.Data.SqlClient
:
var builder = WebApplication.CreateBuilder(args);
builder.Services.AddSqlDataSource("Server=.\\SQLEXPRESS;Database=Northwind;Integrated Security=true;Trust Server Certificate=true");
This registers a transient SqlConnection
which can get injected into your controllers:
app.MapGet("/", async (SqlConnection connection) =>
{
await connection.OpenAsync();
await using var command = new SqlCommand("SELECT TOP 1 SupplierID FROM Suppliers", connection);
return "Hello World: " + await command.ExecuteScalarAsync();
});
But wait! If all you want is to execute some simple SQL, just use the singleton SqlDataSource
to execute a command directly:
app.MapGet("/", async (SqlDataSource dataSource) =>
{
await using var command = dataSource.CreateCommand("SELECT TOP 1 SupplierID FROM Suppliers");
return "Hello World: " + await command.ExecuteScalarAsync();
});
SqlDataSource
can also come in handy when you need more than one connection:
app.MapGet("/", async (SqlDataSource dataSource) =>
{
await using var connection1 = await dataSource.OpenConnectionAsync();
await using var connection2 = await dataSource.OpenConnectionAsync();
// Use the two connections...
});
The AddSqlDataSource method also enables logging of Microsoft.Data.SqlClient
activity in your ASP.NET Core app.
By default informational messages are logged, this can be configured via logging configuration:
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.AspNetCore": "Warning",
"Microsoft.Data.SqlClient": "Warning"
}
}
}
You can also disable SqlClient logging completely like this:
builder.Services.AddSqlDataSource("Server=.\\SQLEXPRESS;Database=Northwind;Integrated Security=true;Trust Server Certificate=true", setupAction =>
{
setupAction.UseLoggerFactory(null);
});
And you can turn on full logging like this:
builder.Services.AddSqlDataSource("Server=.\\SQLEXPRESS;Database=Northwind;Integrated Security=true;Trust Server Certificate=true", setupAction =>
{
setupAction.EnableVerboseLogging();
});
For more information, see the SqlClient documentation.
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net6.0 is compatible. 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 is compatible. 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. |
-
net6.0
- Microsoft.Data.SqlClient (>= 5.1.0)
- Microsoft.Extensions.DependencyInjection.Abstractions (>= 6.0.0)
- Microsoft.Extensions.Logging.Abstractions (>= 6.0.0)
-
net7.0
- Microsoft.Data.SqlClient (>= 5.1.0)
- Microsoft.Extensions.DependencyInjection.Abstractions (>= 6.0.0)
- Microsoft.Extensions.Logging.Abstractions (>= 6.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 |
---|---|---|
1.0.5 | 1,965 | 10/6/2024 |
1.0.4 | 2,413 | 6/5/2024 |
1.0.3 | 3,731 | 1/30/2024 |
1.0.2 | 1,161 | 1/11/2024 |
1.0.1 | 6,371 | 4/9/2023 |
1.0.0-preview1 | 1,879 | 1/20/2023 |
0.1.2-alpha | 2,927 | 8/6/2022 |
0.1.1-alpha | 127 | 8/3/2022 |
Added .NET 6 support