# AspireSqlServerSqlClientExtensions Methods

- Package: [Aspire.Microsoft.Data.SqlClient](/reference/api/csharp/aspire.microsoft.data.sqlclient.md)
- Type: [AspireSqlServerSqlClientExtensions](/reference/api/csharp/aspire.microsoft.data.sqlclient/aspiresqlserversqlclientextensions.md)
- Kind: `Methods`
- Members: `2`

Extension methods for configuring SqlClient connection to Azure SQL, MS SQL server

## AddKeyedSqlServerClient(IHostApplicationBuilder, string, Action<MicrosoftDataSqlClientSettings>)

- Name: `AddKeyedSqlServerClient(IHostApplicationBuilder, string, Action<MicrosoftDataSqlClientSettings>)`
- Modifiers: `extension`
- Source: [GitHub](https://github.com/microsoft/aspire/blob/cbc352350f1a9bafbaff10d14a2c8de4ac186a48/src/Components/Aspire.Microsoft.Data.SqlClient/AspireSqlServerSqlClientExtensions.cs#L45-L48)

Registers 'Scoped' `SqlClient.SqlConnection` factory for given `name` for connecting Azure SQL, MsSQL database using Microsoft.Data.SqlClient. Configures health check, logging and telemetry for the SqlClient.

```csharp
public static class AspireSqlServerSqlClientExtensions
{
    public static void AddKeyedSqlServerClient(
        this IHostApplicationBuilder builder,
        string name,
        Action<MicrosoftDataSqlClientSettings>? configureSettings = null)
    {
        // ...
    }
}
```

## Parameters

- `builder` (`IHostApplicationBuilder`)
  The `Hosting.IHostApplicationBuilder` to read config from and add services to.
- `name` (`string`)
  The name of the component, which is used as the `ServiceDescriptor.ServiceKey` of the service and also to retrieve the connection string from the ConnectionStrings configuration section.
- `configureSettings` (`Action<MicrosoftDataSqlClientSettings>`) `optional`
  An optional method that can be used for customizing options. It's invoked after the settings are read from the configuration.

## Exceptions

- `InvalidOperationException` -- If required [MicrosoftDataSqlClientSettings.ConnectionString](/reference/api/csharp/aspire.microsoft.data.sqlclient/microsoftdatasqlclientsettings/properties.md#connectionstring) is not provided in configuration section.

## Remarks

Reads the configuration from "Aspire:Microsoft:Data:SqlClient:{name}" section.

## AddSqlServerClient(IHostApplicationBuilder, string, Action<MicrosoftDataSqlClientSettings>)

- Name: `AddSqlServerClient(IHostApplicationBuilder, string, Action<MicrosoftDataSqlClientSettings>)`
- Modifiers: `extension`
- Source: [GitHub](https://github.com/microsoft/aspire/blob/cbc352350f1a9bafbaff10d14a2c8de4ac186a48/src/Components/Aspire.Microsoft.Data.SqlClient/AspireSqlServerSqlClientExtensions.cs#L32)

Registers 'Scoped' `SqlClient.SqlConnection` factory for connecting Azure SQL, MS SQL database using Microsoft.Data.SqlClient. Configures health check, logging and telemetry for the SqlClient.

```csharp
public static class AspireSqlServerSqlClientExtensions
{
    public static void AddSqlServerClient(
        this IHostApplicationBuilder builder,
        string connectionName,
        Action<MicrosoftDataSqlClientSettings>? configureSettings = null)
    {
        // ...
    }
}
```

## Parameters

- `builder` (`IHostApplicationBuilder`)
  The `Hosting.IHostApplicationBuilder` to read config from and add services to.
- `connectionName` (`string`)
  A name used to retrieve the connection string from the ConnectionStrings configuration section.
- `configureSettings` (`Action<MicrosoftDataSqlClientSettings>`) `optional`
  An optional delegate that can be used for customizing options. It's invoked after the settings are read from the configuration.

## Exceptions

- `InvalidOperationException` -- If required [MicrosoftDataSqlClientSettings.ConnectionString](/reference/api/csharp/aspire.microsoft.data.sqlclient/microsoftdatasqlclientsettings/properties.md#connectionstring) is not provided in configuration section.

## Remarks

Reads the configuration from "Aspire:Microsoft:Data:SqlClient" section.
