Skip to content

Commit a4c77fa

Browse files
Merge pull request #267040 from wchigit/fix-sql-code
Fix SQL sample code
2 parents a61fd46 + 8ae8993 commit a4c77fa

File tree

3 files changed

+39
-40
lines changed

3 files changed

+39
-40
lines changed

articles/app-service/includes/tutorial-connect-msi-azure-database/code-sql-mi.md

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -85,19 +85,19 @@ For more information, see [Connect using Microsoft Entra authentication](/sql/co
8585
server = os.getenv('AZURE_SQL_SERVER')
8686
port = os.getenv('AZURE_SQL_PORT')
8787
database = os.getenv('AZURE_SQL_DATABASE')
88-
authentication = os.getenv('AZURE_SQL_AUTHENTICATION')
88+
authentication = os.getenv('AZURE_SQL_AUTHENTICATION') # The value should be 'ActiveDirectoryMsi'
8989
9090
# Uncomment the following lines according to the authentication type.
9191
# For system-assigned managed identity.
9292
# connString = f'Driver={{ODBC Driver 18 for SQL Server}};Server={server},{port};Database={database};Authentication={authentication};Encrypt=yes;'
9393
9494
# For user-assigned managed identity.
95-
# user = os.getenv('AZURE_SQL_USER')
96-
# connString = f'Driver={{ODBC Driver 18 for SQL Server}};Server={server},{port};Database={database};UID={user};Authentication={authentication};Encrypt=yes;'
95+
# client_id = os.getenv('AZURE_SQL_USER')
96+
# connString = f'Driver={{ODBC Driver 18 for SQL Server}};Server={server},{port};Database={database};UID={client_id};Authentication={authentication};Encrypt=yes;'
9797
9898
conn = pyodbc.connect(connString)
9999
```
100-
100+
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](/azure/azure-sql/database/azure-sql-passwordless-migration-python).
101101
102102
### [NodeJS](#tab/nodejs)
103103
@@ -135,13 +135,11 @@ For more information, see [Connect using Microsoft Entra authentication](/sql/co
135135
// port,
136136
// database,
137137
// authentication: {
138-
// type: authenticationType,
139-
// options: {
140-
// clientId: clientId
141-
// }
138+
// type: authenticationType
142139
// },
143140
// options: {
144-
// encrypt: true
141+
// encrypt: true,
142+
// clientId: clientId
145143
// }
146144
// };
147145

articles/app-service/tutorial-connect-msi-azure-database.md

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -188,27 +188,31 @@ If you encounter any problem when creating a connection, refer to [Troubleshooti
188188
189189
## 3. Modify your code
190190
191-
In this section, connectivity to the Azure database in your code follows the `DefaultAzureCredential` pattern for all language stacks. `DefaultAzureCredential` is flexible enough to adapt to both the development environment and the Azure environment. When running locally, it can retrieve the logged-in Azure user from the environment of your choice (Visual Studio, Visual Studio Code, Azure CLI, or Azure PowerShell). When running in Azure, it retrieves the managed identity. So it's possible to have connectivity to database both at development time and in production. The pattern is as follows:
192-
193-
1. Instantiate a `DefaultAzureCredential` from the Azure Identity client library. If you're using a user-assigned identity, specify the client ID of the identity.
194-
2. Get an access token for the resource URI respective to the database type.
195-
* For Azure SQL Database: `https://database.windows.net/.default`
196-
* For Azure Database for MySQL: `https://ossrdbms-aad.database.windows.net/.default`
197-
* For Azure Database for PostgreSQL: `https://ossrdbms-aad.database.windows.net/.default`
198-
3. Add the token to your connection string.
199-
4. Open the connection.
200-
201191
# [Azure SQL Database](#tab/sqldatabase-sc)
202192
203-
[!INCLUDE [code sample for postgres managed identity authentication connection](./includes/tutorial-connect-msi-azure-database/code-sql-mi.md)]
193+
[!INCLUDE [code sample for sql managed identity authentication connection](./includes/tutorial-connect-msi-azure-database/code-sql-mi.md)]
204194
205195
# [Azure Database for MySQL](#tab/mysql-sc)
206196
197+
Connectivity to the Azure Database for MySQL in your code follows the `DefaultAzureCredential` pattern for all language stacks. `DefaultAzureCredential` is flexible enough to adapt to both the development environment and the Azure environment. When running locally, it can retrieve the logged-in Azure user from the environment of your choice (Visual Studio, Visual Studio Code, Azure CLI, or Azure PowerShell). When running in Azure, it retrieves the managed identity. So it's possible to have connectivity to database both at development time and in production. The pattern is as follows:
198+
199+
1. Instantiate a `DefaultAzureCredential` from the Azure Identity client library. If you're using a user-assigned identity, specify the client ID of the identity.
200+
2. Get an access token for Azure Database for MySQL: `https://ossrdbms-aad.database.windows.net/.default`.
201+
3. Add the token to your connection string.
202+
4. Open the connection.
203+
207204
[!INCLUDE [code sample for mysql managed identity authentication connection](./includes/tutorial-connect-msi-azure-database/code-mysql-mi.md)]
208205
209206
# [Azure Database for PostgreSQL](#tab/postgresql-sc)
210207
211-
[!INCLUDE [code sample for sql managed identity authentication connection](./includes/tutorial-connect-msi-azure-database/code-postgres-mi.md)]
208+
Connectivity to the Azure Database for PostgreSQL in your code follows the `DefaultAzureCredential` pattern for all language stacks. `DefaultAzureCredential` is flexible enough to adapt to both the development environment and the Azure environment. When running locally, it can retrieve the logged-in Azure user from the environment of your choice (Visual Studio, Visual Studio Code, Azure CLI, or Azure PowerShell). When running in Azure, it retrieves the managed identity. So it's possible to have connectivity to database both at development time and in production. The pattern is as follows:
209+
210+
1. Instantiate a `DefaultAzureCredential` from the Azure Identity client library. If you're using a user-assigned identity, specify the client ID of the identity.
211+
2. Get an access token for Azure Database for PostgreSQL: `https://ossrdbms-aad.database.windows.net/.default`.
212+
3. Add the token to your connection string.
213+
4. Open the connection.
214+
215+
[!INCLUDE [code sample for postgres managed identity authentication connection](./includes/tutorial-connect-msi-azure-database/code-postgres-mi.md)]
212216
213217
-----
214218

articles/service-connector/includes/code-sql-me-id.md

Lines changed: 16 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -85,9 +85,10 @@ Update your application following the tutorial [Migrate a Java application to us
8585
python -m pip install pyodbc
8686
```
8787
88-
1. Get the Azure SQL Database connection configurations from the environment variable added by Service Connector. When using the code below, uncomment the part of the code snippet for the authentication type you want to use.
88+
1. Get the Azure SQL Database connection configurations from the environment variable added by Service Connector. When using the code below, uncomment the part of the code snippet for the authentication type you want to use. If you are using Azure Container Apps as compute service or the connection string in the code snippet doesn't work, refer to [Migrate a Python application to use passwordless connections with Azure SQL Database](/azure/azure-sql/database/azure-sql-passwordless-migration-python) to connect to Azure SQL Database using an access token.
89+
8990
```python
90-
import os;
91+
import os
9192
import pyodbc
9293
9394
server = os.getenv('AZURE_SQL_SERVER')
@@ -97,16 +98,16 @@ Update your application following the tutorial [Migrate a Java application to us
9798
9899
# Uncomment the following lines according to the authentication type.
99100
# For system-assigned managed identity.
100-
# connString = f'Driver={{ODBC Driver 18 for SQL Server}};Server={server},{port};Database={database};Authentication={authentication};Encrypt=yes;'
101+
# connString = f'Driver={{ODBC Driver 18 for SQL Server}};Server=tcp:{server},{port};Database={database};Authentication={authentication};Encrypt=yes;TrustServerCertificate=no;Connection Timeout=30'
101102
102103
# For user-assigned managed identity.
103-
# user = os.getenv('AZURE_SQL_USER')
104-
# connString = f'Driver={{ODBC Driver 18 for SQL Server}};Server={server},{port};Database={database};UID={user};Authentication={authentication};Encrypt=yes;'
104+
# clientID = os.getenv('AZURE_SQL_USER')
105+
# 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'
105106
106107
# For service principal.
107108
# user = os.getenv('AZURE_SQL_USER')
108109
# password = os.getenv('AZURE_SQL_PASSWORD')
109-
# connString = f'Driver={{ODBC Driver 18 for SQL Server}};Server={server},{port};Database={database};UID={user};PWD={password};Authentication={authentication};Encrypt=yes;'
110+
# 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'
110111
111112
conn = pyodbc.connect(connString)
112113
```
@@ -133,7 +134,7 @@ Update your application following the tutorial [Migrate a Java application to us
133134
// port,
134135
// database,
135136
// authentication: {
136-
// authenticationType
137+
// type: authenticationType
137138
// },
138139
// options: {
139140
// encrypt: true
@@ -147,13 +148,11 @@ Update your application following the tutorial [Migrate a Java application to us
147148
// port,
148149
// database,
149150
// authentication: {
150-
// type: authenticationType,
151-
// options: {
152-
// clientId: clientId
153-
// }
151+
// type: authenticationType
154152
// },
155153
// options: {
156-
// encrypt: true
154+
// encrypt: true,
155+
// clientId: clientId
157156
// }
158157
// };
159158
@@ -166,15 +165,13 @@ Update your application following the tutorial [Migrate a Java application to us
166165
// port,
167166
// database,
168167
// authentication: {
169-
// type: authenticationType,
170-
// options: {
171-
// clientId: clientId,
172-
// clientSecret: clientSecret,
173-
// tenantId: tenantId
174-
// }
168+
// type: authenticationType
175169
// },
176170
// options: {
177-
// encrypt: true
171+
// encrypt: true,
172+
// clientId: clientId,
173+
// clientSecret: clientSecret,
174+
// tenantId: tenantId
178175
// }
179176
// };
180177

0 commit comments

Comments
 (0)