Integrare database SQL di Azure con Service Connessione or
Articolo
Questa pagina mostra i metodi e i client di autenticazione supportati e mostra il codice di esempio che è possibile usare per connettere i servizi di calcolo a database SQL di Azure tramite Service Connessione or. È comunque possibile connettersi a database SQL di Azure usando altri metodi. Questa pagina mostra anche i nomi e i valori predefiniti delle variabili di ambiente che si ottengono quando si crea la connessione al servizio.
Servizi di calcolo supportati
Il Connessione or del servizio può essere usato per connettere i servizi di calcolo seguenti a database SQL di Azure:
Servizio app di Azure
Funzioni di Azure
App contenitore di Azure
Azure Spring Apps
Tipi e client di autenticazione supportati
La tabella seguente illustra le combinazioni di metodi di autenticazione e client supportati per la connessione del servizio di calcolo a database SQL di Azure tramite Service Connessione or. Un valore "Sì" indica che la combinazione è supportata, mentre "No" indica che non è supportata.
Tipo client
Identità gestita assegnata dal sistema
Identità gestita assegnata dall'utente
Segreto/stringa di connessione
Entità servizio
.NET
Sì
Sì
Sì
Sì
Go
No
No
Sì
No
Java
Sì
Sì
Sì
Sì
Java - Spring Boot
Sì
Sì
Sì
Sì
Node.js
Sì
Sì
Sì
Sì
PHP
No
No
Sì
No
Python
Sì
Sì
Sì
Sì
Python - Django
No
No
Sì
No
Ruby
No
No
Sì
No
Nessuno
Sì
Sì
Sì
Sì
Questa tabella indica che il metodo Secret/stringa di connessione è supportato per tutti i tipi di client. I metodi Identità gestita assegnata dal sistema, Identità gestita assegnata dall'utente e Entità servizio sono supportati per i tipi di client .NET, Java, Java - Spring Boot, Node.js, Python e None. Questi metodi non sono supportati per i tipi di client Go, PHP, Django e Ruby.
Nota
L'identità gestita assegnata dal sistema, l'identità gestita assegnata dall'utente e l'entità servizio sono supportate solo nell'interfaccia della riga di comando di Azure.
Nomi di variabili di ambiente predefiniti o proprietà dell'applicazione e codice di esempio
Usare i dettagli di connessione seguenti per connettere i servizi di calcolo a database SQL di Azure. Per ogni esempio seguente, sostituire i testi <sql-server>segnaposto , <sql-database>, <sql-username>e <sql-password> con il nome del server, il nome del database, l'ID utente e la password. Per altre informazioni sulle convenzioni di denominazione, vedere l'articolo Servizi Connessione or internals .
Ottenere il database SQL di Azure stringa di connessione dalla variabile di ambiente aggiunta da Service Connessione or.
using Microsoft.Data.SqlClient;
string connectionString =
Environment.GetEnvironmentVariable("AZURE_SQL_CONNECTIONSTRING")!;
using var connection = new SqlConnection(connectionString);
connection.Open();
Ottenere il database SQL di Azure stringa di connessione dalla variabile di ambiente aggiunta da Service Connessione or.
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;"
// For service principal: "jdbc:sqlserver://{SQLName}.database.windows.net:1433;databaseName={SQLDbName};user={ServicePrincipalClientId};password={spSecret};authentication=ActiveDirectoryServicePrincipal;"
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();
}
}
}
Per un'applicazione Spring, se si crea una connessione con l'opzione --client-type springboot, Service Connessione or imposta le proprietà spring.datasource.url con formato jdbc:sqlserver://<sql-server>.database.windows.net:1433;databaseName=<sql-db>;authentication=ActiveDirectoryMSI; valore su Azure Spring Apps.
Ottenere le configurazioni di connessione database SQL di Azure dalla variabile di ambiente aggiunta da Service Connessione or. Quando si usa il codice seguente, rimuovere il commento dalla parte del frammento di codice per il tipo di autenticazione che si vuole usare.
import os;
import pyodbc
server = os.getenv('AZURE_SQL_SERVER')
port = os.getenv('AZURE_SQL_PORT')
database = os.getenv('AZURE_SQL_DATABASE')
authentication = os.getenv('AZURE_SQL_AUTHENTICATION')
# Uncomment the following lines according to the authentication type.
# For system-assigned managed identity.
# connString = f'Driver={{ODBC Driver 18 for SQL Server}};Server={server},{port};Database={database};Authentication={authentication};Encrypt=yes;'
# For user-assigned managed identity.
# user = os.getenv('AZURE_SQL_USER')
# connString = f'Driver={{ODBC Driver 18 for SQL Server}};Server={server},{port};Database={database};UID={user};Authentication={authentication};Encrypt=yes;'
# For service principal.
# user = os.getenv('AZURE_SQL_USER')
# password = os.getenv('AZURE_SQL_PASSWORD')
# connString = f'Driver={{ODBC Driver 18 for SQL Server}};Server={server},{port};Database={database};UID={user};PWD={password};Authentication={authentication};Encrypt=yes;'
conn = pyodbc.connect(connString)
Installare le dipendenze.
npm install mssql
Ottenere le configurazioni di connessione database SQL di Azure dalle variabili di ambiente aggiunte da Service Connessione or. Quando si usa il codice seguente, rimuovere il commento dalla parte del frammento di codice per il tipo di autenticazione che si vuole usare.
Per altre lingue, usare le proprietà di connessione impostate da Service Connessione or sulle variabili di ambiente per connettere il database. Per informazioni dettagliate sulle variabili di ambiente, vedere Integrare database SQL di Azure con Service Connessione or.
Data Source=<sql-server>.database.windows.net,1433;Initial Catalog=<sql-database>;User ID=<identity-client-ID>;Authentication=ActiveDirectoryManagedIdentity
Ottenere il database SQL di Azure stringa di connessione dalla variabile di ambiente aggiunta da Service Connessione or.
using Microsoft.Data.SqlClient;
string connectionString =
Environment.GetEnvironmentVariable("AZURE_SQL_CONNECTIONSTRING")!;
using var connection = new SqlConnection(connectionString);
connection.Open();
Ottenere il database SQL di Azure stringa di connessione dalla variabile di ambiente aggiunta da Service Connessione or.
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;"
// For service principal: "jdbc:sqlserver://{SQLName}.database.windows.net:1433;databaseName={SQLDbName};user={ServicePrincipalClientId};password={spSecret};authentication=ActiveDirectoryServicePrincipal;"
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();
}
}
}
Per un'applicazione Spring, se si crea una connessione con l'opzione --client-type springboot, Service Connessione or imposta le proprietà spring.datasource.url con formato jdbc:sqlserver://<sql-server>.database.windows.net:1433;databaseName=<sql-db>;authentication=ActiveDirectoryMSI; valore su Azure Spring Apps.
Ottenere le configurazioni di connessione database SQL di Azure dalla variabile di ambiente aggiunta da Service Connessione or. Quando si usa il codice seguente, rimuovere il commento dalla parte del frammento di codice per il tipo di autenticazione che si vuole usare.
import os;
import pyodbc
server = os.getenv('AZURE_SQL_SERVER')
port = os.getenv('AZURE_SQL_PORT')
database = os.getenv('AZURE_SQL_DATABASE')
authentication = os.getenv('AZURE_SQL_AUTHENTICATION')
# Uncomment the following lines according to the authentication type.
# For system-assigned managed identity.
# connString = f'Driver={{ODBC Driver 18 for SQL Server}};Server={server},{port};Database={database};Authentication={authentication};Encrypt=yes;'
# For user-assigned managed identity.
# user = os.getenv('AZURE_SQL_USER')
# connString = f'Driver={{ODBC Driver 18 for SQL Server}};Server={server},{port};Database={database};UID={user};Authentication={authentication};Encrypt=yes;'
# For service principal.
# user = os.getenv('AZURE_SQL_USER')
# password = os.getenv('AZURE_SQL_PASSWORD')
# connString = f'Driver={{ODBC Driver 18 for SQL Server}};Server={server},{port};Database={database};UID={user};PWD={password};Authentication={authentication};Encrypt=yes;'
conn = pyodbc.connect(connString)
Installare le dipendenze.
npm install mssql
Ottenere le configurazioni di connessione database SQL di Azure dalle variabili di ambiente aggiunte da Service Connessione or. Quando si usa il codice seguente, rimuovere il commento dalla parte del frammento di codice per il tipo di autenticazione che si vuole usare.
Per altre lingue, usare le proprietà di connessione impostate da Service Connessione or sulle variabili di ambiente per connettere il database. Per informazioni dettagliate sulle variabili di ambiente, vedere Integrare database SQL di Azure con Service Connessione or.
Ottenere il database SQL di Azure stringa di connessione dalla variabile di ambiente aggiunta da Service Connessione or.
using Microsoft.Data.SqlClient;
string connectionString =
Environment.GetEnvironmentVariable("AZURE_SQL_CONNECTIONSTRING")!;
using var connection = new SqlConnection(connectionString);
connection.Open();
Aggiungere le dipendenze seguenti nel file pom.xml :
Configurare l'applicazione Spring. Le configurazioni di connessione vengono aggiunte a Spring Apps by Service Connessione or.
Installare le dipendenze.
python -m pip install pyodbc
Ottenere le configurazioni di connessione database SQL di Azure dalla variabile di ambiente aggiunta da Service Connessione or.
import os;
import pyodbc
server = os.getenv('AZURE_SQL_SERVER')
port = os.getenv('AZURE_SQL_PORT')
database = os.getenv('AZURE_SQL_DATABASE')
user = os.getenv('AZURE_SQL_USER')
password = os.getenv('AZURE_SQL_PASSWORD')
connString = f'Driver={{ODBC Driver 18 for SQL Server}};Server={server},{port};Database={database};UID={user};PWD={password};Encrypt=yes;TrustServerCertificate=no;Connection Timeout=30'
conn = pyodbc.connect(connString)
Installare le dipendenze.
pip install django
pip install pyodbc
Nel file di impostazione ottenere le configurazioni di connessione database SQL di Azure dalla variabile di ambiente aggiunta da Service Connessione or.
# in your setting file, eg. settings.py
server = os.getenv('AZURE_SQL_HOST')
port = os.getenv('AZURE_SQL_PORT')
database = os.getenv('AZURE_SQL_NAME')
user = os.getenv('AZURE_SQL_USER')
password = os.getenv('AZURE_SQL_PASSWORD')
DATABASES = {
'default': {
'ENGINE': 'sql_server.pyodbc',
'NAME': databse,
'USER': user,
'PASSWORD': password,
'HOST': server,
'PORT': port,
'OPTIONS': {
'driver': 'ODBC Driver 13 for SQL Server',
},
},
}
Installare la dipendenza.
go install github.com/microsoft/go-mssqldb@latest
Ottenere il database SQL di Azure stringa di connessione dalla variabile di ambiente aggiunta da Service Connessione or.
Per altre lingue, usare le proprietà di connessione impostate da Service Connessione or sulle variabili di ambiente per connettere il database. Per informazioni dettagliate sulle variabili di ambiente, vedere Integrare database SQL di Azure con Service Connessione or.
Data Source=<sql-server>.database.windows.net,1433;Initial Catalog=<sql-database>;User ID=a30eeedc-e75f-4301-b1a9-56e81e0ce99c;Password=asdfghwerty;Authentication=ActiveDirectoryServicePrincipal
Ottenere il database SQL di Azure stringa di connessione dalla variabile di ambiente aggiunta da Service Connessione or.
using Microsoft.Data.SqlClient;
string connectionString =
Environment.GetEnvironmentVariable("AZURE_SQL_CONNECTIONSTRING")!;
using var connection = new SqlConnection(connectionString);
connection.Open();
Ottenere il database SQL di Azure stringa di connessione dalla variabile di ambiente aggiunta da Service Connessione or.
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;"
// For service principal: "jdbc:sqlserver://{SQLName}.database.windows.net:1433;databaseName={SQLDbName};user={ServicePrincipalClientId};password={spSecret};authentication=ActiveDirectoryServicePrincipal;"
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();
}
}
}
Per un'applicazione Spring, se si crea una connessione con l'opzione --client-type springboot, Service Connessione or imposta le proprietà spring.datasource.url con formato jdbc:sqlserver://<sql-server>.database.windows.net:1433;databaseName=<sql-db>;authentication=ActiveDirectoryMSI; valore su Azure Spring Apps.
Ottenere le configurazioni di connessione database SQL di Azure dalla variabile di ambiente aggiunta da Service Connessione or. Quando si usa il codice seguente, rimuovere il commento dalla parte del frammento di codice per il tipo di autenticazione che si vuole usare.
import os;
import pyodbc
server = os.getenv('AZURE_SQL_SERVER')
port = os.getenv('AZURE_SQL_PORT')
database = os.getenv('AZURE_SQL_DATABASE')
authentication = os.getenv('AZURE_SQL_AUTHENTICATION')
# Uncomment the following lines according to the authentication type.
# For system-assigned managed identity.
# connString = f'Driver={{ODBC Driver 18 for SQL Server}};Server={server},{port};Database={database};Authentication={authentication};Encrypt=yes;'
# For user-assigned managed identity.
# user = os.getenv('AZURE_SQL_USER')
# connString = f'Driver={{ODBC Driver 18 for SQL Server}};Server={server},{port};Database={database};UID={user};Authentication={authentication};Encrypt=yes;'
# For service principal.
# user = os.getenv('AZURE_SQL_USER')
# password = os.getenv('AZURE_SQL_PASSWORD')
# connString = f'Driver={{ODBC Driver 18 for SQL Server}};Server={server},{port};Database={database};UID={user};PWD={password};Authentication={authentication};Encrypt=yes;'
conn = pyodbc.connect(connString)
Installare le dipendenze.
npm install mssql
Ottenere le configurazioni di connessione database SQL di Azure dalle variabili di ambiente aggiunte da Service Connessione or. Quando si usa il codice seguente, rimuovere il commento dalla parte del frammento di codice per il tipo di autenticazione che si vuole usare.
Per altre lingue, usare le proprietà di connessione impostate da Service Connessione or sulle variabili di ambiente per connettere il database. Per informazioni dettagliate sulle variabili di ambiente, vedere Integrare database SQL di Azure con Service Connessione or.