Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/erikej/sqlclientextensions
.NET dependency injection and logging extensions for Microsoft.Data.SqlClient
https://github.com/erikej/sqlclientextensions
dotnet sql-server
Last synced: 7 days ago
JSON representation
.NET dependency injection and logging extensions for Microsoft.Data.SqlClient
- Host: GitHub
- URL: https://github.com/erikej/sqlclientextensions
- Owner: ErikEJ
- License: mit
- Created: 2022-08-01T08:23:47.000Z (over 2 years ago)
- Default Branch: main
- Last Pushed: 2024-12-17T06:25:37.000Z (about 1 month ago)
- Last Synced: 2025-01-17T01:07:09.501Z (14 days ago)
- Topics: dotnet, sql-server
- Language: C#
- Homepage:
- Size: 110 KB
- Stars: 47
- Watchers: 3
- Forks: 2
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# ErikEJ.SqlClient.Extensions
Adds support for .NET Dependency Injection and Logging for Microsoft.Data.SqlClient.
## Installation
[![NuGet](https://img.shields.io/nuget/v/ErikEJ.SqlClient.Extensions)](https://www.nuget.org/packages/ErikEJ.SqlClient.Extensions)
Install the latest package from [NuGet](https://www.nuget.org/packages/ErikEJ.SqlClient.Extensions).
## Getting started
[`Microsoft.Data.SqlClient`](https://github.com/dotnet/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`:
```csharp
var builder = WebApplication.CreateBuilder(args);builder.Services.AddSqlDataSource("Server=.\\SQLEXPRESS;Database=Northwind;Integrated Security=true;Trust Server Certificate=true");
```This registers a transient [`SqlConnection`](https://docs.microsoft.com/dotnet/api/microsoft.data.sqlclient.sqlconnection) which can get injected into your controllers:
```csharp
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:
```csharp
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:
```csharp
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:
```json
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.AspNetCore": "Warning",
"Microsoft.Data.SqlClient": "Warning"
}
}
}
```You can also disable SqlClient logging completely like this:
```csharp
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:
```csharp
builder.Services.AddSqlDataSource("Server=.\\SQLEXPRESS;Database=Northwind;Integrated Security=true;Trust Server Certificate=true", setupAction =>
{
setupAction.EnableVerboseLogging();
});
```For more information, [see the SqlClient documentation](https://docs.microsoft.com/sql/connect/ado-net/introduction-microsoft-data-sqlclient-namespace).