Connect to SQL Server
Konten ini belum tersedia dalam bahasa Anda.
This page describes how consuming apps connect to a SQL Server resource that’s already modeled in your AppHost. For the AppHost API surface — adding a SQL Server instance, databases, data volumes, bind mounts, and more — see SQL Server Hosting integration.
When you reference a SQL Server resource from your AppHost, Aspire injects the connection information into the consuming app as environment variables. Your app can either read those environment variables directly — the pattern works the same from any language — or, in C#, use the Aspire SQL Server client integration for automatic dependency injection, health checks, and telemetry.
Connection properties
Section titled “Connection properties”Aspire exposes each property as an environment variable named [RESOURCE]_[PROPERTY]. For instance, the Uri property of a resource called sqldb becomes SQLDB_URI.
SQL Server server resource
Section titled “SQL Server server resource”The SQL Server server resource exposes the following connection properties:
| Property Name | Description |
|---|---|
Host | The hostname or IP address of the SQL Server instance |
Port | The port number the SQL Server instance is listening on |
Username | The username for authentication |
Password | The password for authentication |
Uri | The connection URI in mssql:// format, with the format mssql://{Username}:{Password}@{Host}:{Port} |
JdbcConnectionString | JDBC-format connection string, with the format jdbc:sqlserver://{Host}:{Port};trustServerCertificate=true. User and password credentials are provided as separate Username and Password properties. |
Example connection strings:
Uri: mssql://sa:p%40ssw0rd1@localhost:1433JdbcConnectionString: jdbc:sqlserver://localhost:1433;trustServerCertificate=trueSQL Server database resource
Section titled “SQL Server database resource”The SQL Server database resource inherits all properties from its parent server resource and adds:
| Property Name | Description |
|---|---|
Uri | The connection URI with the database name, with the format mssql://{Username}:{Password}@{Host}:{Port}/{DatabaseName} |
JdbcConnectionString | JDBC connection string with the database name, with the format jdbc:sqlserver://{Host}:{Port};trustServerCertificate=true;databaseName={DatabaseName}. User and password credentials are provided as separate Username and Password properties. |
DatabaseName | The name of the database |
Example connection strings:
Uri: mssql://sa:p%40ssw0rd1@localhost:1433/catalogJdbcConnectionString: jdbc:sqlserver://localhost:1433;trustServerCertificate=true;databaseName=catalogConnect from your app
Section titled “Connect from your app”Pick the language your consuming app is written in. Each example assumes your AppHost adds a SQL Server database resource named sqldb and references it from the consuming app.