| author | xfz11 |
|---|---|
| ms.service | service-connector |
| ms.topic | include |
| ms.date | 04/17/2024 |
| ms.author | xiaofanzhou |
| ms.custom | sfi-ropc-nochange |
-
Install dependencies.
dotnet add package Microsoft.Data.SqlClient
-
Get the Azure SQL Database connection string from the environment variable added by Service Connector.
using Microsoft.Data.SqlClient; // AZURE_SQL_CONNECTIONSTRING should be one of the following: // For system-assigned managed identity:"Server=tcp:<server-name>.database.windows.net;Database=<database-name>;Authentication=Active Directory Default;TrustServerCertificate=True" // For user-assigned managed identity: "Server=tcp:<server-name>.database.windows.net;Database=<database-name>;Authentication=Active Directory Default;User Id=<client-id-of-user-assigned-identity>;TrustServerCertificate=True" string connectionString = Environment.GetEnvironmentVariable("AZURE_SQL_CONNECTIONSTRING")!; using var connection = new SqlConnection(connectionString); connection.Open();
For more information, see Using Active Directory Managed Identity authentication.
-
Add the following dependencies in your pom.xml file:
<dependency> <groupId>com.azure</groupId> <artifactId>azure-identity</artifactId> <version>1.4.6</version> </dependency> <dependency> <groupId>com.microsoft.sqlserver</groupId> <artifactId>mssql-jdbc</artifactId> <version>10.2.0.jre11</version> </dependency>
-
Get the Azure SQL Database connection string from the environment variable added by Service Connector.
import java.sql.Connection; import java.sql.ResultSet; import java.sql.Statement; import com.microsoft.sqlserver.jdbc.SQLServerDataSource; public class Main { public static void main(String[] args) { // AZURE_SQL_CONNECTIONSTRING should be one of the following: // For system-assigned managed identity: "jdbc:sqlserver://{SQLName}.database.windows.net:1433;databaseName={SQLDbName};authentication=ActiveDirectoryMSI;" // For user-assigned managed identity: "jdbc:sqlserver://{SQLName}.database.windows.net:1433;databaseName={SQLDbName};msiClientId={UserAssignedMiClientId};authentication=ActiveDirectoryMSI;" String connectionString = System.getenv("AZURE_SQL_CONNECTIONSTRING"); SQLServerDataSource ds = new SQLServerDataSource(); ds.setURL(connectionString); try (Connection connection = ds.getConnection()) { System.out.println("Connected successfully."); } catch (SQLException e) { e.printStackTrace(); } } }
For more information, see Connect using Microsoft Entra authentication.
-
Install dependencies.
python -m pip install mssql-python python-dotenv
-
Get the Azure SQL Database connection configurations from the environment variable added by Service Connector. Uncomment the part of the code snippet for the authentication type you want to use.
import os from mssql_python import connect server = os.getenv('AZURE_SQL_SERVER') port = os.getenv('AZURE_SQL_PORT') database = os.getenv('AZURE_SQL_DATABASE') # Uncomment the following lines according to the authentication type. # For system-assigned managed identity. # connection_string = f'Server={server},{port};Database={database};Authentication=ActiveDirectoryMSI;Encrypt=yes;' # For user-assigned managed identity. # client_id = os.getenv('AZURE_SQL_USER') # connection_string = f'Server={server},{port};Database={database};UID={client_id};Authentication=ActiveDirectoryMSI;Encrypt=yes;' conn = connect(connection_string)
For an alternative method, you can also connect to Azure SQL Database using an access token, refer to Migrate a Python application to use passwordless connections with Azure SQL Database.
- Install dependencies.
npm install mssql
- Get the Azure SQL Database connection configurations from the environment variables added by Service Connector. Uncomment the part of the code snippet for the authentication type you want to use.
import sql from 'mssql'; const server = process.env.AZURE_SQL_SERVER; const database = process.env.AZURE_SQL_DATABASE; const port = parseInt(process.env.AZURE_SQL_PORT); const authenticationType = process.env.AZURE_SQL_AUTHENTICATIONTYPE; // Uncomment the following lines according to the authentication type. // For system-assigned managed identity. // const config = { // server, // port, // database, // authentication: { // authenticationType // }, // options: { // encrypt: true // } // }; // For user-assigned managed identity. // const clientId = process.env.AZURE_SQL_CLIENTID; // const config = { // server, // port, // database, // authentication: { // type: authenticationType // }, // options: { // encrypt: true, // clientId: clientId // } // }; this.poolconnection = await sql.connect(config);
For more information, see Homepage for client programming to Microsoft SQL Server. For more code samples, see Create a passwordless connection to a database service via Service Connector.