| author | chentony |
|---|---|
| ms.service | service-connector |
| ms.topic | include |
| ms.date | 02/27/2025 |
| ms.author | chentony |
-
Install dependencies.
dotnet add package Microsoft.Data.SqlClient
-
Retrieve the SQL database in Microsoft Fabric connection string from the environment variable added by Service Connector.
using Microsoft.Data.SqlClient; string connectionString = Environment.GetEnvironmentVariable("FABRIC_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.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>
-
Retrieve the SQL database in Microsoft Fabric 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) { // FABRIC_SQL_CONNECTIONSTRING should be one of the following: // For system-assigned managed identity: "jdbc:sqlserver://<Fabric-SQL-Identifier>.msit-database.fabric.microsoft.com,1433;databaseName=<SQL-DB-name>-<Fabric-DB-Identifier>;authentication=ActiveDirectoryMSI;" // For user-assigned managed identity: "jdbc:sqlserver://<Fabric-SQL-Identifier>.msit-database.fabric.microsoft.com,1433;databaseName=<SQL-DB-name>-<Fabric-DB-Identifier>;msiClientId=<msiClientId>;authentication=ActiveDirectoryMSI;" String connectionString = System.getenv("FABRIC_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 to Azure databases from App Service without secrets using a managed identity.
For a Spring application, if you create a connection with option --client-type springboot, Service Connector sets the environment variable FABRIC_SQL_CONNECTIONSTRING with value format jdbc:sqlserver://<Fabric-SQL-Identifier>.msit-database.fabric.microsoft.com,1433;databaseName=<SQL-DB-name>-<Fabric-DB-Identifier>;authentication=ActiveDirectoryMSI; to Azure Spring Apps.
For user-assigned managed identities, msiClientId=<msiClientId>; is added.
Update your application following the tutorial Migrate a Java application to use passwordless connections with Azure SQL Database. Remember to remove the spring.datasource.password configuration property if it was previously set and add the correct dependencies.
spring:
datasource:
url: ${FABRIC_SQL_CONNECTIONSTRING}-
Install dependencies.
python -m pip install mssql-python python-dotenv
-
Retrieve the SQL database in Microsoft Fabric connection string from the environment variable added by Service Connector.
Authentication=ActiveDirectoryMSI;is required in the connection string when connecting using managed identities.UID=<msiClientId>is also required in the connection string when connecting using a user-assigned managed identity.import os from mssql_python import connect connection_string = os.getenv('FABRIC_SQL_CONNECTIONSTRING') # System-assigned managed identity connection string format # `Server=tcp:<Fabric-SQL-Identifier>.msit-database.fabric.microsoft.com,1433;Database=<SQL-DB-name>-<Fabric-DB-Identifier>;Authentication=ActiveDirectoryMSI;` # User-assigned managed identity connection string format # `Server=tcp:<Fabric-SQL-Identifier>.msit-database.fabric.microsoft.com,1433;Database=<SQL-DB-name>-<Fabric-DB-Identifier>;UID=<msiClientId>;Authentication=ActiveDirectoryMSI;` conn = connect(connection_string)
- Install dependencies.
go mod init <YourProjectName> go mod tidy
- Retrieve the SQL database in Microsoft Fabric connection string from the environment variable added by Service Connector.
package main import ( "github.com/microsoft/go-mssqldb/azuread" "database/sql" "context" "log" "fmt" "os" ) var db *sql.DB var connectionString = os.Getenv("FABRIC_SQL_CONNECTIONSTRING") func main() { var err error // Create connection pool db, err = sql.Open(azuread.DriverName, connectionString) if err != nil { log.Fatal("Error creating connection pool: ", err.Error()) } ctx := context.Background() err = db.PingContext(ctx) if err != nil { log.Fatal(err.Error()) } fmt.Printf("Connected!\n") }
For more information, see Use Golang to query a database in Azure SQL Database.
For other languages, use the connection string that Service Connector sets to the environment variables to connect the database. For environment variable details, see Integrate SQL database in Microsoft Fabric with Service Connector.
For more information, see Connect to your SQL database in Microsoft Fabric.