Skip to content

Implement SqlDataSource #2119

Open

Description

@roji

.NET 7.0 introduced the new DbDataSource abstraction to System.Data (issue), here's a video discussing it in depth.

Now, the .NET 7.0 implementation provides a default implementation which can be obtained from the ADO.NET provider's DbProviderFactory without the driver needing to implement anything. So the following code already works today:

await using var dataSource = SqlClientFactory.Instance.CreateDataSource("<connection string>");
await using var connection = await dataSource.OpenConnectionAsync();
await using var command = connection.CreateCommand();
command.CommandText = "SELECT 1";
Console.WriteLine(command.ExecuteScalar());

However, this provides a database-agnostic DbDataSource which isn't typed to any specific provider, so it hands out DbConnections rather than SqlConnections, which may be cumbersome to work with if the user is only targeting SQL Server.

A proper SqlDataSource implementation in SqlClient would solve this, and also provide a good configuration/setup point (e.g. for authentication/access tokens, rather than the current static methods).

Activity

  1. JRahnama commented on Aug 11, 2023

    @JRahnama
    Contributor

    Thanks @roji. We will discuss this internally and will get back to you.

  2. roji commented on Aug 11, 2023

    @roji
    MemberAuthor

    Yep, something like that. Npgsql has an NpgsqlDataSourceBuilder type for configuring an NpgsqlDataSource (when you're done you call Build() on it and get your data source); that would be the type of the setupAction parameter in your lambda above. There's also an Npgsql.DependencyInjection package which does precisely this kind of nice integration with DI that you're showing above.

    NpgsqlDataSourceBuilder has all manner of configuration on it: stuff for managing access tokens, tweaking type mappings, logging and telemetry configuration, etc. Basically anything that you need to pre-configure for all the connections you'll be using from that data source. This is a much better alternative than static configuration, which is what you have to do if there's no data source and users are directly instantiating SqlConnections.

    (see the video I posted above for more discussion on how this is useful etc.)

  3. roji commented on Aug 11, 2023

    @roji
    MemberAuthor

    Note that as a first step, I'd just implement SqlDataSource so that users can use it as a simple connection factory (e.g. to be registered in DI). Adding actual support for e.g. access token management is additional work on top of that which would require a bit more refactoring inside SqlClient to support.

  4. ErikEJ commented on Aug 11, 2023

    @ErikEJ
    Contributor

    Cool, I might look into that, since I already published a package which does that 😄

  5. ErikEJ commented on Aug 11, 2023

    @ErikEJ
    Contributor

    @roji I presume then something like ErikEJ.Data.SqlClient.DependencyInjection would be next (and maybe eventually Microsoft.Data.SqlClient.DependencyInjection) ?

  6. roji commented on Aug 11, 2023

    @roji
    MemberAuthor

    Yeah, exactly. SqlDataSource is something that needs to be implemented inside SqlClient (since ideally it would go on to support actual SqlClient-specific configurations like around access tokens). A DI integration package - similar to Npgsql.DependencyInjection - could be external (e.g. ErikEJ...), though ideally it would indeed be Microsoft.Data.SqlClient.DependencyInjection at some point.

  7. adamsitnik commented on Dec 8, 2023

    @adamsitnik
    Member

    Once it gets implemented, AspNetCore.HealthChecks.SqlServer (a very popular health check package for SQL Server with 37 milion downloads) should start using it too. I've created Xabaril/AspNetCore.Diagnostics.HealthChecks#2123 to track it.

  8. wasabii commented on Sep 25, 2025

    @wasabii
  9. roji commented on Sep 26, 2025

    @roji
    Author
  10. moved this from To triage to Backlog in SqlClient Boardon Nov 21, 2025
  11. priyankatiwari08 commented on Sep 17, 2026

    @priyankatiwari08
    Contributor

    /triage

  12. github-actions commented on Sep 17, 2026

    @github-actions

    🔍 Triage Summary (on-demand re-triage)

    Check Result
    Issue type Feature
    Environment N/A (feature request, not a bug)
    Area Area\Engineering
    Duplicates None found (related closed items: #2631 "Allow PoolingDataSource creation via SqlDataSourceBuilder", #2626 "Add a PoolingDataSource to SqlClientX", #2659 "Move DataSource parsing logic" — none fully implement the public SqlDataSource/SqlDataSourceBuilder API surface requested here)
    Regression Not applicable

    Analysis

    This is a long-standing feature request (opened 2023-08-10) asking SqlClient to implement a first-class SqlDataSource/SqlDataSourceBuilder type analogous to Npgsql's NpgsqlDataSource/NpgsqlDataSourceBuilder, so consumers get a strongly-typed connection factory (rather than the generic DbDataSource obtainable via SqlClientFactory.Instance.CreateDataSource) and a natural extension point for future config such as access-token management. Comments indicate related exploratory work exists in a branch/SqlClientX prototype (see closed #2626/#2631/#2659), but no public SqlDataSource API has shipped yet. Given sustained community interest (9 👍, repeated pings over 2+ years, and downstream consumers like AspNetCore.HealthChecks.SqlServer waiting on it), this should be tracked as a substantial API-design feature. Severity/priority: P2 (valuable DX/API improvement, not a defect, no urgent customer impact blocking usage today since DbDataSource already works as a workaround).

    Next Steps

    • This requires public API design work (new SqlDataSource / SqlDataSourceBuilder types) — per .github/instructions/api-design.instructions.md, any new public surface must be reflected in the ref/ reference assembly projects and reviewed for API-design consistency before merging.
    • Recommend reviewing the prior related/closed issues (Add a PoolingDataSource to SqlClientX #2626, Allow PoolingDataSource creation via SqlDataSourceBuilder #2631, Move DataSource parsing logic from DataSource to DataSourceParserHandler #2659) and any existing SqlClientX prototype branch mentioned in comments to avoid duplicating design work and to reuse groundwork already done.
    • Given the scope (new public types, connection-pooling integration, and potential auth/token configuration hooks), this is a good candidate for a design proposal/RFC before implementation rather than a direct Copilot coding agent assignment. Once a design is agreed, implementation and tests could be delegated to Copilot coding agent for the core SqlDataSource/SqlDataSourceBuilder scaffolding.
    • No label change made for this on-demand /triage run (existing labels preserved).

    Note: This triage summary is auto-generated by an AI agent. The analysis and suggestions above have not been verified by a human maintainer. Please treat as preliminary guidance only.

    Generated by SqlClient Issue Auto-Triage for #2119 · copilot · auto · 37.7 AIC · ⌖ 14.6 AIC · ⊞ 11.9K · ◷

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions