{"id":15410395,"url":"https://github.com/erikej/sqlclientextensions","last_synced_at":"2025-04-05T16:06:40.131Z","repository":{"id":50751171,"uuid":"520024959","full_name":"ErikEJ/SqlClientExtensions","owner":"ErikEJ","description":".NET dependency injection and logging extensions for Microsoft.Data.SqlClient","archived":false,"fork":false,"pushed_at":"2025-03-25T01:21:07.000Z","size":127,"stargazers_count":48,"open_issues_count":0,"forks_count":3,"subscribers_count":3,"default_branch":"main","last_synced_at":"2025-03-29T15:06:55.115Z","etag":null,"topics":["dotnet","sql-server"],"latest_commit_sha":null,"homepage":"","language":"C#","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/ErikEJ.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null},"funding":{"github":"ErikEJ"}},"created_at":"2022-08-01T08:23:47.000Z","updated_at":"2025-03-25T19:00:24.000Z","dependencies_parsed_at":"2024-01-16T08:06:53.309Z","dependency_job_id":"20917e3a-e03e-422b-baee-9a24157536dd","html_url":"https://github.com/ErikEJ/SqlClientExtensions","commit_stats":{"total_commits":70,"total_committers":2,"mean_commits":35.0,"dds":"0.48571428571428577","last_synced_commit":"256aa7bfaf09d6e08dd8f0de3baf7b8e7e8b3073"},"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ErikEJ%2FSqlClientExtensions","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ErikEJ%2FSqlClientExtensions/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ErikEJ%2FSqlClientExtensions/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ErikEJ%2FSqlClientExtensions/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/ErikEJ","download_url":"https://codeload.github.com/ErikEJ/SqlClientExtensions/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247361686,"owners_count":20926643,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["dotnet","sql-server"],"created_at":"2024-10-01T16:44:15.109Z","updated_at":"2025-04-05T16:06:40.106Z","avatar_url":"https://github.com/ErikEJ.png","language":"C#","funding_links":["https://github.com/sponsors/ErikEJ"],"categories":[],"sub_categories":[],"readme":"# ErikEJ.SqlClient.Extensions\n\nAdds support for .NET Dependency Injection and Logging for Microsoft.Data.SqlClient.\n\n## Installation\n\n[![NuGet](https://img.shields.io/nuget/v/ErikEJ.SqlClient.Extensions)](https://www.nuget.org/packages/ErikEJ.SqlClient.Extensions)\n\nInstall the latest package from [NuGet](https://www.nuget.org/packages/ErikEJ.SqlClient.Extensions).\n\n## Getting started\n\n[`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.\n\nThis 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).\n\nFor example, if using the ASP.NET minimal web API, simply use the following to register `Microsoft.Data.SqlClient`:\n\n```csharp\nvar builder = WebApplication.CreateBuilder(args);\n\nbuilder.Services.AddSqlDataSource(\"Server=.\\\\SQLEXPRESS;Database=Northwind;Integrated Security=true;Trust Server Certificate=true\");\n```\n\nThis registers a transient [`SqlConnection`](https://docs.microsoft.com/dotnet/api/microsoft.data.sqlclient.sqlconnection) which can get injected into your controllers:\n\n```csharp\napp.MapGet(\"/\", async (SqlConnection connection) =\u003e\n{\n    await connection.OpenAsync();\n    await using var command = new SqlCommand(\"SELECT TOP 1 SupplierID FROM Suppliers\", connection);\n    return \"Hello World: \" + await command.ExecuteScalarAsync();\n});\n```\n\nBut wait! If all you want is to execute some simple SQL, just use the singleton `SqlDataSource` to execute a command directly:\n\n```csharp\napp.MapGet(\"/\", async (SqlDataSource dataSource) =\u003e\n{\n    await using var command = dataSource.CreateCommand(\"SELECT TOP 1 SupplierID FROM Suppliers\");\n    return \"Hello World: \" + await command.ExecuteScalarAsync();\n});\n```\n\n`SqlDataSource` can also come in handy when you need more than one connection:\n\n```csharp\napp.MapGet(\"/\", async (SqlDataSource dataSource) =\u003e\n{\n    await using var connection1 = await dataSource.OpenConnectionAsync();\n    await using var connection2 = await dataSource.OpenConnectionAsync();\n    // Use the two connections...\n});\n```\n\nThe AddSqlDataSource method also enables logging of `Microsoft.Data.SqlClient` activity in your ASP.NET Core app.\n\nBy default informational messages are logged, this can be configured via logging configuration:\n\n```json\n{\n  \"Logging\": {\n    \"LogLevel\": {\n      \"Default\": \"Information\",\n      \"Microsoft.AspNetCore\": \"Warning\",\n      \"Microsoft.Data.SqlClient\": \"Warning\"\n    }\n  }\n}\n```\n\nYou can also disable SqlClient logging completely like this:\n\n```csharp\n   builder.Services.AddSqlDataSource(\"Server=.\\\\SQLEXPRESS;Database=Northwind;Integrated Security=true;Trust Server Certificate=true\", setupAction =\u003e\n   {\n       setupAction.UseLoggerFactory(null);\n   });\n```\n\nAnd you can turn on full logging like this:\n\n```csharp\n   builder.Services.AddSqlDataSource(\"Server=.\\\\SQLEXPRESS;Database=Northwind;Integrated Security=true;Trust Server Certificate=true\", setupAction =\u003e\n   {\n       setupAction.EnableVerboseLogging();\n   });\n```\n\nFor more information, [see the SqlClient documentation](https://docs.microsoft.com/sql/connect/ado-net/introduction-microsoft-data-sqlclient-namespace).\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ferikej%2Fsqlclientextensions","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ferikej%2Fsqlclientextensions","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ferikej%2Fsqlclientextensions/lists"}