Skip to content

[Proposal] It should be possible to manually create SqlException #35

Description

@ashmind

SqlException create methods are currently internal.
I would like some kind of create method to be public.

Rationale

Unit tests. There are various scenarios in which SqlException is handled differently than other kinds of exceptions, and being able to test those scenarios without actual database would be useful.

Related StackOverflow thread, How to throw a SqlException when needed for mocking and unit testing?, has 53 upvotes.

Potential API

class SqlException {
    // ...
    public SqlException(
        string message,
        SqlErrorCollection errorCollection,
        Guid clientConnectionId = Guid.Empty,
        Exception innerException = null
    )  { ... }
}

class SqlErrorCollection {
    // ...
    public SqlErrorCollection()  { ... }
    public SqlErrorCollection(params SqlError[] errors)  { ... }
}

class SqlError {
    // ...
    public SqlError(
        int infoNumber,
        byte errorState,
        byte errorClass,
        string server,
        string errorMessage,
        string procedure,
        int lineNumber
    ) { ... }
}

Open Questions

Is there a better way to expose SqlError that would allow easier future extensibility?
Some kind of SqlErrorBuilder?

Activity

  1. divega commented on May 16, 2019

    @divega

    As recently announced in the .NET Blog, focus on new SqlClient features an improvements is moving to the new Microsoft.Data.SqlClient package. For this reason, we are moving this issue to the new repo at https://github.com/dotnet/SqlClient. We will still use https://github.com/dotnet/corefx to track issues on other providers like System.Data.Odbc and System.Data.OleDB, and general ADO.NET and .NET data access issues.

  2. transferred this issue fromdotnet/corefxon May 16, 2019
  3. moved this from To triage to Backlog in SqlClient Boardon Nov 21, 2025
  4. added
    Approved
    on Nov 21, 2025
  5. MosheSteinberg commented on Aug 3, 2026

    @MosheSteinberg

    Is there any chance of this ticket being picked up at some point? I see it is still open, but no activity for years.
    Thank you for maintaining the package!

  6. priyankatiwari08 commented on Sep 17, 2026

    @priyankatiwari08
    Contributor

    /triage

  7. MosheSteinberg commented on Sep 29, 2026

    @MosheSteinberg

    That attempt to call a triage command didn't succeed

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

    Approved

    Projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions