此頁面會顯示支援的驗證方法和用戶端,並顯示範例程式碼,您可以用來將計算服務連線到使用服務連接器的 Azure SQL Database。 您可能仍可以使用其他方法連線到 Azure SQL Database。 此頁面也顯示您在建立服務連線時取得的預設環境變數名稱和值。
支援的計算服務
服務連接器可用來將下列計算服務連線至 Azure SQL Database:
- Azure App Service
- Azure 容器應用程式
- Azure Functions
- Azure Kubernetes Service (AKS)
- Azure Spring Apps
支援的驗證類型和用戶端
下表顯示使用服務連接器將計算服務連線到 Azure SQL Database 時,支援哪些驗證方法和用戶端組合。 「是」表示支援的組合,而「否」則表示不支援。
| 用戶端類型 |
系統指派的受控識別 |
使用者指派的受控識別 |
祕密/連接字串 |
服務主體 |
| .NET |
Yes |
.是 |
.是 |
Yes |
| Go |
No |
無 |
.是 |
No |
| Java |
Yes |
.是 |
.是 |
Yes |
| Java - Spring Boot |
Yes |
.是 |
.是 |
Yes |
| Node.js |
Yes |
.是 |
.是 |
Yes |
| PHP |
No |
無 |
.是 |
No |
| Python |
Yes |
.是 |
.是 |
Yes |
| Python - Django |
No |
無 |
.是 |
No |
| Ruby |
No |
無 |
.是 |
No |
| 無 |
Yes |
.是 |
.是 |
Yes |
下表指出所有用戶端類型都支援祕密/連接字串方法。 .NET、JAVA、JAVA - Spring Boot、Node.js、Python 和 None 用戶端類型支援系統指派的受控識別、使用者指派的受控識別和服務主體方法。 Go、PHP、Django 和 Ruby 用戶端類型不支援這些方法。
注意
系統指派的受控識別、使用者指派的受控識別和服務主體僅在 Azure CLI 中受到支援。
預設環境變數名稱或應用程式屬性和範例程式碼
使用下列連線詳細資料,將計算服務連線到 Azure SQL Database。 在下列每個範例中,以您的伺服器名稱、資料庫名稱、使用者名稱和密碼取代預留位置文字<sql-server>、<sql-database>、<sql-username> 和 <sql-password>。 如需命名慣例的詳細資訊,請參閱服務連接器內部一文。
系統指派的受控識別
| 預設環境變數名稱 |
描述 |
範例值 |
AZURE_SQL_CONNECTIONSTRING |
Azure SQL Database 連接字串 |
Data Source=<sql-server>.database.windows.net,1433;Initial Catalog=<sql-database>;Authentication=ActiveDirectoryManagedIdentity |
| 預設環境變數名稱 |
描述 |
範例值 |
AZURE_SQL_CONNECTIONSTRING |
Azure SQL Database 連接字串 |
jdbc:sqlserver://<sql-server>.database.windows.net:1433;databaseName=<sql-database>;authentication=ActiveDirectoryMSI; |
| 預設環境變數名稱 |
描述 |
範例值 |
spring.datasource.url |
Azure SQL Database 資料來源 URL |
jdbc:sqlserver://<sql-server>.database.windows.net:1433;databaseName=<sql-db>;authentication=ActiveDirectoryMSI; |
| 預設環境變數名稱 |
描述 |
範例值 |
AZURE_SQL_SERVER |
Azure SQL Database 伺服器 |
<sql-server>.database.windows.net |
AZURE_SQL_PORT |
Azure SQL Database 連接埠 |
1433 |
AZURE_SQL_DATABASE |
Azure SQL Database 資料庫 |
<sql-database> |
AZURE_SQL_AUTHENTICATION |
Azure SQL 驗證 |
ActiveDirectoryMsi |
| 預設環境變數名稱 |
描述 |
範例值 |
AZURE_SQL_SERVER |
Azure SQL Database 伺服器 |
<sql-server>.database.windows.net |
AZURE_SQL_PORT |
Azure SQL Database 連接埠 |
1433 |
AZURE_SQL_DATABASE |
Azure SQL Database 資料庫 |
<sql-database> |
AZURE_SQL_AUTHENTICATIONTYPE |
Azure SQL Database 驗證類型 |
azure-active-directory-default |
| 預設環境變數名稱 |
描述 |
範例值 |
AZURE_SQL_HOST |
Azure SQL Database 伺服器 |
<sql-server>.database.windows.net |
AZURE_SQL_PORT |
Azure SQL Database 連接埠 |
1433 |
AZURE_SQL_DATABASE |
Azure SQL Database 資料庫 |
<sql-database> |
AZURE_SQL_AUTHENTICATION |
Azure SQL Database 驗證類型 |
azure-active-directory-default |
範例指令碼
請參閱下面的步驟和程式碼,以使用系統指派的受控識別來連線到 Azure SQL Database。
安裝相依性。
dotnet add package Microsoft.Data.SqlClient
從服務連接器新增的環境變數取得 Azure SQL Database 連結字串。
using Microsoft.Data.SqlClient;
string connectionString =
Environment.GetEnvironmentVariable("AZURE_SQL_CONNECTIONSTRING")!;
using var connection = new SqlConnection(connectionString);
connection.Open();
如需詳細資訊,請參閱使用 Active Directory 受控識別驗證。
在您的 pom.xml 中新增下列相依性:
<dependency>
<groupId>com.microsoft.sqlserver</groupId>
<artifactId>mssql-jdbc</artifactId>
<version>10.2.0.jre11</version>
</dependency>
<dependency>
<groupId>com.azure</groupId>
<artifactId>azure-identity</artifactId>
<version>1.7.0</version>
</dependency>
從服務連接器新增的環境變數取得 Azure SQL Database 連結字串。
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();
}
}
}
如需詳細資訊,請參閱使用無祕密的受控識別從 App Service 連線到 Azure 資料庫。
在 Spring 應用程式中,如果您使用選項 --client-type springboot 建立連線,服務連接器會將具有值格式 jdbc:sqlserver://<sql-server>.database.windows.net:1433;databaseName=<sql-db>;authentication=ActiveDirectoryMSI; 的屬性 spring.datasource.url 設定為 Azure Spring 應用程式。
依照教學課程移轉 JAVA 應用程式以使用無密碼連線和 Azure SQL Database 來更新應用程式。 如果之前已設定 spring.datasource.password 設定屬性,請記得要移除它,然後新增正確的相依性。
安裝相依性。
python -m pip install pyodbc
從服務連接器新增的環境變數取得 Azure SQL Database 連結組態。 使用下列程式代碼時,請取消註解您想要使用的驗證類型代碼段部分。 如果您正在使用 Azure 容器應用程式作為計算服務,或是程式碼片段中的連接字串無法運作,請參閱移轉 Python 應用程式以使用無密碼連線和 Azure SQL Database 來使用存取權杖連線到 Azure SQL Database。
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 corresponding to the authentication type you want to use.
# For system-assigned managed identity.
# connString = f'Driver={{ODBC Driver 18 for SQL Server}};Server=tcp:{server},{port};Database={database};Authentication={authentication};Encrypt=yes;TrustServerCertificate=no;Connection Timeout=30'
# For user-assigned managed identity.
# clientID = os.getenv('AZURE_SQL_USER')
# connString = f'Driver={{ODBC Driver 18 for SQL Server}};Server=tcp:{server},{port};Database={database};UID={clientID};Authentication={authentication};Encrypt=yes;TrustServerCertificate=no;Connection Timeout=30'
# 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=tcp:{server},{port};Database={database};UID={user};PWD={password};Authentication={authentication};Encrypt=yes;TrustServerCertificate=no;Connection Timeout=30'
conn = pyodbc.connect(connString)
- 安裝相依性。
npm install mssql
- 從服務連接器新增的環境變數取得 Azure SQL Database 連結設定。 使用下列程式代碼時,請取消註解您想要使用的驗證類型代碼段部分。
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 corresponding to the authentication type you want to use.
// For system-assigned managed identity.
// const config = {
// server,
// port,
// database,
// authentication: {
// type: 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
// }
// };
// For service principal.
// const clientId = process.env.AZURE_SQL_CLIENTID;
// const clientSecret = process.env.AZURE_SQL_CLIENTSECRET;
// const tenantId = process.env.AZURE_SQL_TENANTID;
// const config = {
// server,
// port,
// database,
// authentication: {
// type: authenticationType
// },
// options: {
// encrypt: true,
// clientId: clientId,
// clientSecret: clientSecret,
// tenantId: tenantId
// }
// };
this.poolconnection = await sql.connect(config);
如需更多資訊,請參閱 Microsoft SQL Server 用戶端程式設計的首頁。
使用者指派的受控識別
| 預設環境變數名稱 |
描述 |
範例值 |
AZURE_SQL_CONNECTIONSTRING |
Azure SQL Database 連接字串 |
Data Source=<sql-server>.database.windows.net,1433;Initial Catalog=<sql-database>;User ID=<identity-client-ID>;Authentication=ActiveDirectoryManagedIdentity |
| 預設環境變數名稱 |
描述 |
範例值 |
AZURE_SQL_CONNECTIONSTRING |
Azure SQL Database 連接字串 |
jdbc:sqlserver://<sql-server>.database.windows.net:1433;databaseName=<sql-database>;msiClientId=<msiClientId>;authentication=ActiveDirectoryMSI; |
| 預設環境變數名稱 |
描述 |
範例值 |
spring.datasource.url |
Azure SQL Database 資料來源 URL |
jdbc:sqlserver://<sql-server>.database.windows.net:1433;databaseName=<sql-db>;msiClientId=<msiClientId>;authentication=ActiveDirectoryMSI; |
| 預設環境變數名稱 |
描述 |
範例值 |
AZURE_SQL_SERVER |
Azure SQL Database 伺服器 |
<sql-server>.database.windows.net |
AZURE_SQL_PORT |
Azure SQL Database 連接埠 |
1433 |
AZURE_SQL_DATABASE |
Azure SQL Database 資料庫 |
<sql-database> |
AZURE_SQL_USER |
Azure SQL Database 使用者 |
Object (principal) ID |
AZURE_SQL_AUTHENTICATION |
Azure SQL 驗證 |
ActiveDirectoryMsi |
| 預設環境變數名稱 |
描述 |
範例值 |
AZURE_SQL_SERVER |
Azure SQL Database 伺服器 |
<sql-server>.database.windows.net |
AZURE_SQL_PORT |
Azure SQL Database 連接埠 |
1433 |
AZURE_SQL_DATABASE |
Azure SQL Database 資料庫 |
<sql-database> |
AZURE_SQL_AUTHENTICATIONTYPE |
Azure SQL Database 驗證類型 |
azure-active-directory-default |
AZURE_SQL_CLIENTID |
Azure SQL Database 用戶端識別碼 |
<identity-client-ID> |
| 預設環境變數名稱 |
描述 |
範例值 |
AZURE_SQL_HOST |
Azure SQL Database 伺服器 |
<sql-server>.database.windows.net |
AZURE_SQL_PORT |
Azure SQL Database 連接埠 |
1433 |
AZURE_SQL_DATABASE |
Azure SQL Database 資料庫 |
<sql-database> |
AZURE_SQL_AUTHENTICATION |
Azure SQL Database 驗證類型 |
azure-active-directory-default |
AZURE_SQL_USERNAME |
Azure SQL Database 用戶端識別碼 |
<your Client ID> |
範例指令碼
請參閱下面的步驟和程式碼,以使用使用者指派的受控識別來連線到 Azure SQL Database。
安裝相依性。
dotnet add package Microsoft.Data.SqlClient
從服務連接器新增的環境變數取得 Azure SQL Database 連結字串。
using Microsoft.Data.SqlClient;
string connectionString =
Environment.GetEnvironmentVariable("AZURE_SQL_CONNECTIONSTRING")!;
using var connection = new SqlConnection(connectionString);
connection.Open();
如需詳細資訊,請參閱使用 Active Directory 受控識別驗證。
在您的 pom.xml 中新增下列相依性:
<dependency>
<groupId>com.microsoft.sqlserver</groupId>
<artifactId>mssql-jdbc</artifactId>
<version>10.2.0.jre11</version>
</dependency>
<dependency>
<groupId>com.azure</groupId>
<artifactId>azure-identity</artifactId>
<version>1.7.0</version>
</dependency>
從服務連接器新增的環境變數取得 Azure SQL Database 連結字串。
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();
}
}
}
如需詳細資訊,請參閱使用無祕密的受控識別從 App Service 連線到 Azure 資料庫。
在 Spring 應用程式中,如果您使用選項 --client-type springboot 建立連線,服務連接器會將具有值格式 jdbc:sqlserver://<sql-server>.database.windows.net:1433;databaseName=<sql-db>;authentication=ActiveDirectoryMSI; 的屬性 spring.datasource.url 設定為 Azure Spring 應用程式。
依照教學課程移轉 JAVA 應用程式以使用無密碼連線和 Azure SQL Database 來更新應用程式。 如果之前已設定 spring.datasource.password 設定屬性,請記得要移除它,然後新增正確的相依性。
安裝相依性。
python -m pip install pyodbc
從服務連接器新增的環境變數取得 Azure SQL Database 連結組態。 使用下列程式代碼時,請取消註解您想要使用的驗證類型代碼段部分。 如果您正在使用 Azure 容器應用程式作為計算服務,或是程式碼片段中的連接字串無法運作,請參閱移轉 Python 應用程式以使用無密碼連線和 Azure SQL Database 來使用存取權杖連線到 Azure SQL Database。
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 corresponding to the authentication type you want to use.
# For system-assigned managed identity.
# connString = f'Driver={{ODBC Driver 18 for SQL Server}};Server=tcp:{server},{port};Database={database};Authentication={authentication};Encrypt=yes;TrustServerCertificate=no;Connection Timeout=30'
# For user-assigned managed identity.
# clientID = os.getenv('AZURE_SQL_USER')
# connString = f'Driver={{ODBC Driver 18 for SQL Server}};Server=tcp:{server},{port};Database={database};UID={clientID};Authentication={authentication};Encrypt=yes;TrustServerCertificate=no;Connection Timeout=30'
# 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=tcp:{server},{port};Database={database};UID={user};PWD={password};Authentication={authentication};Encrypt=yes;TrustServerCertificate=no;Connection Timeout=30'
conn = pyodbc.connect(connString)
- 安裝相依性。
npm install mssql
- 從服務連接器新增的環境變數取得 Azure SQL Database 連結設定。 使用下列程式代碼時,請取消註解您想要使用的驗證類型代碼段部分。
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 corresponding to the authentication type you want to use.
// For system-assigned managed identity.
// const config = {
// server,
// port,
// database,
// authentication: {
// type: 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
// }
// };
// For service principal.
// const clientId = process.env.AZURE_SQL_CLIENTID;
// const clientSecret = process.env.AZURE_SQL_CLIENTSECRET;
// const tenantId = process.env.AZURE_SQL_TENANTID;
// const config = {
// server,
// port,
// database,
// authentication: {
// type: authenticationType
// },
// options: {
// encrypt: true,
// clientId: clientId,
// clientSecret: clientSecret,
// tenantId: tenantId
// }
// };
this.poolconnection = await sql.connect(config);
如需更多資訊,請參閱 Microsoft SQL Server 用戶端程式設計的首頁。
連接字串
警告
Microsoft 建議您使用最安全的可用驗證流程。 這個程序描述的驗證流程需要在應用程式中具備極高的信任度,且伴隨著其他流程並未面臨的風險。 請僅在其他較安全的流程 (例如受控身分識別) 皆不具可行性的情況下,才使用這個流程。
| 預設環境變數名稱 |
描述 |
範例值 |
AZURE_SQL_CONNECTIONSTRING |
Azure SQL Database 連接字串 |
Data Source=<sql-server>.database.windows.net,1433;Initial Catalog=<sql-database>;Password=<sql-password> |
| 預設環境變數名稱 |
描述 |
範例值 |
AZURE_SQL_CONNECTIONSTRING |
Azure SQL Database 連接字串 |
jdbc:sqlserver://<sql-server>.database.windows.net:1433;databaseName=<sql-database>;user=<sql-username>;password=<sql-password>; |
| 預設環境變數名稱 |
描述 |
範例值 |
spring.datasource.url |
Azure SQL Database 資料來源 URL |
jdbc:sqlserver://<sql-server>.database.windows.net:1433;databaseName=<sql-db>; |
spring.datasource.username |
Azure SQL Database 資料來源使用者名稱 |
<sql-user> |
spring.datasource.password |
Azure SQL Database 資料來源密碼 |
<sql-password> |
| 預設環境變數名稱 |
描述 |
範例值 |
AZURE_SQL_SERVER |
Azure SQL Database 伺服器 |
<sql-server>.database.windows.net |
AZURE_SQL_PORT |
Azure SQL Database 連接埠 |
1433 |
AZURE_SQL_DATABASE |
Azure SQL Database 資料庫 |
<sql-database> |
AZURE_SQL_USER |
Azure SQL Database 使用者 |
<sql-username> |
AZURE_SQL_PASSWORD |
Azure SQL Database 密碼 |
<sql-password> |
| 預設環境變數名稱 |
描述 |
範例值 |
AZURE_SQL_HOST |
Azure SQL Database 主機 |
<sql-server>.database.windows.net |
AZURE_SQL_PORT |
Azure SQL Database 連接埠 |
1433 |
AZURE_SQL_NAME |
Azure SQL Database 名稱 |
<sql-database> |
AZURE_SQL_USER |
Azure SQL Database 使用者 |
<sql-username> |
AZURE_SQL_PASSWORD |
Azure SQL Database 密碼 |
<sql-password> |
| 預設環境變數名稱 |
描述 |
範例值 |
AZURE_SQL_CONNECTIONSTRING |
Azure SQL Database 連接字串 |
server=<sql-server>.database.windows.net;port=1433;database=<sql-database>;user id=<sql-username>;password=<sql-password>; |
| 預設環境變數名稱 |
描述 |
範例值 |
AZURE_SQL_SERVER |
Azure SQL Database 伺服器 |
<sql-server>.database.windows.net |
AZURE_SQL_PORT |
Azure SQL Database 連接埠 |
1433 |
AZURE_SQL_DATABASE |
Azure SQL Database 資料庫 |
<sql-database> |
AZURE_SQL_USERNAME |
Azure SQL Database 使用者名稱 |
<sql-username> |
AZURE_SQL_PASSWORD |
Azure SQL Database 密碼 |
<sql-password> |
| 預設環境變數名稱 |
描述 |
範例值 |
AZURE_SQL_SERVERNAME |
Azure SQL Database 伺服器名稱 |
<sql-server>.database.windows.net,1433 |
AZURE_SQL_DATABASE |
Azure SQL Database 資料庫 |
<sql-database> |
AZURE_SQL_UID |
Azure SQL Database 唯一識別碼 (UID) |
<sql-username> |
AZURE_SQL_PASSWORD |
Azure SQL Database 密碼 |
<sql-password> |
| 預設環境變數名稱 |
描述 |
範例值 |
AZURE_SQL_HOST |
Azure SQL Database 主機 |
<sql-server>.database.windows.net |
AZURE_SQL_PORT |
Azure SQL Database 連接埠 |
1433 |
AZURE_SQL_DATABASE |
Azure SQL Database 資料庫 |
<sql-database> |
AZURE_SQL_USERNAME |
Azure SQL Database 使用者名稱 |
<sql-username> |
AZURE_SQL_PASSWORD |
Azure SQL Database 密碼 |
<sql-password> |
| 預設環境變數名稱 |
描述 |
範例值 |
AZURE_SQL_HOST |
Azure SQL Database 主機 |
<sql-server>.database.windows.net |
AZURE_SQL_PORT |
Azure SQL Database 連接埠 |
1433 |
AZURE_SQL_DATABASE |
Azure SQL Database 資料庫 |
<sql-database> |
AZURE_SQL_USERNAME |
Azure SQL Database 使用者名稱 |
<sql-username> |
AZURE_SQL_PASSWORD |
Azure SQL Database 密碼 |
<sql-password> |
範例指令碼
請參閱下面的步驟和程式碼,以使用連接字串來連線到 Azure SQL Database。
安裝相依性。
dotnet add package Microsoft.Data.SqlClient
從服務連接器新增的環境變數取得 Azure SQL Database 連結字串。
using Microsoft.Data.SqlClient;
string connectionString =
Environment.GetEnvironmentVariable("AZURE_SQL_CONNECTIONSTRING")!;
using var connection = new SqlConnection(connectionString);
connection.Open();
在您的 pom.xml 中新增下列相依性:
<dependency>
<groupId>com.microsoft.sqlserver</groupId>
<artifactId>mssql-jdbc</artifactId>
<version>10.2.0.jre11</version>
</dependency>
從服務連接器新增的環境變數取得 Azure SQL Database 連結字串。
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) {
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();
}
}
}
- 在 'pom.xml' 檔案中新增相依性:
<dependencyManagement>
<dependencies>
<dependency>
<groupId>com.azure.spring</groupId>
<artifactId>spring-cloud-azure-dependencies</artifactId>
<version>5.20.0</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
- 設定 Spring 應用程式。 服務連接器會將連線設定新增至 Spring 應用程式。
安裝相依性。
python -m pip install pyodbc
從服務連接器新增的環境變數取得 Azure SQL Database 連結組態。
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)
安裝相依性。
pip install django
pip install pyodbc
在設定檔案中,從服務連接器新增的環境變數取得 Azure SQL Database 連線設定。
# 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',
},
},
}
安裝相依性。
go install github.com/microsoft/go-mssqldb@latest
從服務連接器新增的環境變數取得 Azure SQL Database 連結字串。
import (
"context"
"database/sql"
"fmt"
"log"
"github.com/microsoft/go-mssqldb/azuread"
)
connectionString := os.Getenv("AZURE_SQL_CONNECTIONSTRING")
db, err = sql.Open(azuread.DriverName, connString)
if err != nil {
log.Fatal("Error creating connection pool: " + err.Error())
}
log.Printf("Connected!\n")
- 安裝相依性。
npm install mssql
- 從服務連接器新增的環境變數取得 Azure SQL Database 連結設定。
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 username = process.env.AZURE_SQL_USERNAME;
const password = process.env.AZURE_SQL_PASSWORD;
const config = {
server,
port,
database,
user,
password,
options: {
encrypt: true
}
};
this.poolconnection = await sql.connect(config);
下載 Microsoft Drivers for PHP for SQL Server。 如需詳細資訊,請參閱用於 SQL Server 之 PHP 的 Microsoft 驅動程式使用者入門。
從服務連接器新增的環境變數取得 Azure SQL Database 連結設定。
<?php
$server = getenv("AZURE_SQL_SERVERNAME");
$database = getenv("AZURE_SQL_DATABASE");
$user = getenv("AZURE_SQL_UID");
$password = getenv("AZURE_SQL_PASSWORD");
$connectionOptions = array(
"Database" => database,
"Uid" => user,
"PWD" => password
);
$conn = sqlsrv_connect($serverName, $connectionOptions);
?>
下載 Ruby Driver for SQL Server。 如需詳細資訊,請參閱設定 Ruby 開發的開發環境。
從服務連接器新增的環境變數取得 Azure SQL Database 連結設定。
client = TinyTds::Client.new username: ENV['AZURE_SQL_USERNAME'], password: ENV['AZURE_SQL_PASSWORD'],
host: ENV['AZURE_SQL_HOST'], port: ENV['AZURE_SQL_PORT'],
database: ENV['AZURE_SQL_DATABASE'], azure:true
如需更多資訊,請參閱 Microsoft SQL Server 用戶端程式設計的首頁。
服務主體
| 預設環境變數名稱 |
描述 |
範例值 |
AZURE_SQL_CLIENTID |
您的用戶端識別碼 |
<client-ID> |
AZURE_SQL_CLIENTSECRET |
您的用戶端密碼 |
<client-secret> |
AZURE_SQL_TENANTID |
您的租用戶識別碼 |
<tenant-ID> |
AZURE_SQL_CONNECTIONSTRING |
Azure SQL Database 連接字串 |
Data Source=<sql-server>.database.windows.net,1433;Initial Catalog=<sql-database>;User ID=<client-Id>;Password=<client-secret>;Authentication=ActiveDirectoryServicePrincipal |
| 預設環境變數名稱 |
描述 |
範例值 |
AZURE_SQL_CONNECTIONSTRING |
Azure SQL Database 連接字串 |
jdbc:sqlserver://<sql-server>.database.windows.net:1433;databaseName=<sql-database>;user=<client-Id>;password=<client-secret>;authentication=ActiveDirectoryServicePrincipal; |
| 預設環境變數名稱 |
描述 |
範例值 |
spring.datasource.url |
Azure SQL Database 資料來源 URL |
jdbc:sqlserver://<sql-server>.database.windows.net:1433;databaseName=<sql-db>;authentication=ActiveDirectoryServicePrincipal; |
spring.datasource.username |
Azure SQL Database 資料來源使用者名稱 |
<client-Id> |
spring.datasource.password |
Azure SQL Database 資料來源密碼 |
<client-Secret> |
| 預設環境變數名稱 |
描述 |
範例值 |
AZURE_SQL_SERVER |
Azure SQL Database 伺服器 |
<sql-server>.database.windows.net |
AZURE_SQL_PORT |
Azure SQL Database 連接埠 |
1433 |
AZURE_SQL_DATABASE |
Azure SQL Database 資料庫 |
<sql-database> |
AZURE_SQL_USER |
Azure SQL Database 使用者 |
your Client Id |
AZURE_SQL_AUTHENTICATION |
Azure SQL 驗證 |
ActiveDirectoryServerPrincipal |
AZURE_SQL_PASSWORD |
Azure SQL Database 密碼 |
your Client Secret |
| 預設環境變數名稱 |
描述 |
範例值 |
AZURE_SQL_SERVER |
Azure SQL Database 伺服器 |
<sql-server>.database.windows.net |
AZURE_SQL_PORT |
Azure SQL Database 連接埠 |
1433 |
AZURE_SQL_DATABASE |
Azure SQL Database 資料庫 |
<sql-database> |
AZURE_SQL_AUTHENTICATIONTYPE |
Azure SQL Database 驗證類型 |
azure-active-directory-default |
AZURE_SQL_CLIENTID |
Azure SQL Database 用戶端識別碼 |
<your Client ID> |
AZURE_SQL_CLIENTSECRET |
Azure SQL Database 用戶端秘密 |
<your Client Secret > |
AZURE_SQL_TENANTID |
Azure SQL Database 租用戶識別碼 |
<your Tenant ID> |
| 預設環境變數名稱 |
描述 |
範例值 |
AZURE_SQL_HOST |
Azure SQL Database 伺服器 |
<sql-server>.database.windows.net |
AZURE_SQL_PORT |
Azure SQL Database 連接埠 |
1433 |
AZURE_SQL_DATABASE |
Azure SQL Database 資料庫 |
<sql-database> |
AZURE_SQL_AUTHENTICATION |
Azure SQL Database 驗證類型 |
azure-active-directory-default |
AZURE_SQL_USERNAME |
Azure SQL Database 用戶端識別碼 |
<your Client ID> |
AZURE_SQL_PASSWORD |
Azure SQL Database 用戶端秘密 |
<your Client Secret > |
範例指令碼
請參閱下面的步驟和程式碼,以使用服務主體來連線到 Azure SQL Database。
安裝相依性。
dotnet add package Microsoft.Data.SqlClient
從服務連接器新增的環境變數取得 Azure SQL Database 連結字串。
using Microsoft.Data.SqlClient;
string connectionString =
Environment.GetEnvironmentVariable("AZURE_SQL_CONNECTIONSTRING")!;
using var connection = new SqlConnection(connectionString);
connection.Open();
如需詳細資訊,請參閱使用 Active Directory 受控識別驗證。
在您的 pom.xml 中新增下列相依性:
<dependency>
<groupId>com.microsoft.sqlserver</groupId>
<artifactId>mssql-jdbc</artifactId>
<version>10.2.0.jre11</version>
</dependency>
<dependency>
<groupId>com.azure</groupId>
<artifactId>azure-identity</artifactId>
<version>1.7.0</version>
</dependency>
從服務連接器新增的環境變數取得 Azure SQL Database 連結字串。
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();
}
}
}
如需詳細資訊,請參閱使用無祕密的受控識別從 App Service 連線到 Azure 資料庫。
在 Spring 應用程式中,如果您使用選項 --client-type springboot 建立連線,服務連接器會將具有值格式 jdbc:sqlserver://<sql-server>.database.windows.net:1433;databaseName=<sql-db>;authentication=ActiveDirectoryMSI; 的屬性 spring.datasource.url 設定為 Azure Spring 應用程式。
依照教學課程移轉 JAVA 應用程式以使用無密碼連線和 Azure SQL Database 來更新應用程式。 如果之前已設定 spring.datasource.password 設定屬性,請記得要移除它,然後新增正確的相依性。
安裝相依性。
python -m pip install pyodbc
從服務連接器新增的環境變數取得 Azure SQL Database 連結組態。 使用下列程式代碼時,請取消註解您想要使用的驗證類型代碼段部分。 如果您正在使用 Azure 容器應用程式作為計算服務,或是程式碼片段中的連接字串無法運作,請參閱移轉 Python 應用程式以使用無密碼連線和 Azure SQL Database 來使用存取權杖連線到 Azure SQL Database。
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 corresponding to the authentication type you want to use.
# For system-assigned managed identity.
# connString = f'Driver={{ODBC Driver 18 for SQL Server}};Server=tcp:{server},{port};Database={database};Authentication={authentication};Encrypt=yes;TrustServerCertificate=no;Connection Timeout=30'
# For user-assigned managed identity.
# clientID = os.getenv('AZURE_SQL_USER')
# connString = f'Driver={{ODBC Driver 18 for SQL Server}};Server=tcp:{server},{port};Database={database};UID={clientID};Authentication={authentication};Encrypt=yes;TrustServerCertificate=no;Connection Timeout=30'
# 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=tcp:{server},{port};Database={database};UID={user};PWD={password};Authentication={authentication};Encrypt=yes;TrustServerCertificate=no;Connection Timeout=30'
conn = pyodbc.connect(connString)
- 安裝相依性。
npm install mssql
- 從服務連接器新增的環境變數取得 Azure SQL Database 連結設定。 使用下列程式代碼時,請取消註解您想要使用的驗證類型代碼段部分。
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 corresponding to the authentication type you want to use.
// For system-assigned managed identity.
// const config = {
// server,
// port,
// database,
// authentication: {
// type: 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
// }
// };
// For service principal.
// const clientId = process.env.AZURE_SQL_CLIENTID;
// const clientSecret = process.env.AZURE_SQL_CLIENTSECRET;
// const tenantId = process.env.AZURE_SQL_TENANTID;
// const config = {
// server,
// port,
// database,
// authentication: {
// type: authenticationType
// },
// options: {
// encrypt: true,
// clientId: clientId,
// clientSecret: clientSecret,
// tenantId: tenantId
// }
// };
this.poolconnection = await sql.connect(config);
如需更多資訊,請參閱 Microsoft SQL Server 用戶端程式設計的首頁。
下一步
請透過下方列出的教學課程深入了解服務連接器。