Skip to content

Latest commit

 

History

History
229 lines (184 loc) · 6.91 KB

File metadata and controls

229 lines (184 loc) · 6.91 KB
author wchigit
ms.service service-connector
ms.topic include
ms.date 11/28/2023
ms.author wchi
  1. Install dependencies.

    dotnet add package Microsoft.Data.SqlClient
  2. Get the Azure SQL Database connection string from the environment variable added by Service Connector.

    using Microsoft.Data.SqlClient;
    
    string connectionString = 
        Environment.GetEnvironmentVariable("AZURE_SQL_CONNECTIONSTRING")!;
    
    using var connection = new SqlConnection(connectionString);
    connection.Open();
  1. 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>
  2. 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) {
            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();
            }
        }
    }
  1. Add dependency in your 'pom.xml' file:
    <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>
  2. Set up the Spring application. The connection configurations are added to Spring Apps by Service Connector.
  1. Install dependencies.

    python -m pip install mssql-python python-dotenv
  2. Get the Azure SQL Database connection configurations from the environment variable added by Service Connector.

    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')
    user = os.getenv('AZURE_SQL_USER')
    password = os.getenv('AZURE_SQL_PASSWORD')
    
    connection_string = f'Server={server},{port};Database={database};UID={user};PWD={password};Encrypt=yes;TrustServerCertificate=no;Connection Timeout=30'
    
    conn = connect(connection_string)
  1. Install dependencies.

    pip install django
    pip install mssql-django pyodbc
  2. In the setting file, get the Azure SQL Database connection configurations from the environment variable added by Service Connector.

    # 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': 'mssql',
            'NAME': database,
            'USER': user,
            'PASSWORD': password,
            'HOST': server,
            'PORT': port,
            'OPTIONS': {
                'driver': 'ODBC Driver 18 for SQL Server',
            },
        },
    }
  1. Install dependency.

    go install github.com/microsoft/go-mssqldb@latest
  2. Get the Azure SQL Database connection string from the environment variable added by Service Connector.

    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")
  1. Install dependencies.
    npm install mssql
  2. Get the Azure SQL Database connection configurations from the environment variables added by Service Connector.
    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);
  1. Download the Microsoft Drivers for PHP for SQL Server. For more information, check Getting Started with the Microsoft Drivers for PHP for SQL Server.

  2. Get the Azure SQL Database connection configurations from the environment variables added by Service Connector.

    <?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);
    ?>
  1. Download Ruby Driver for SQL Server. For more information, check Configure development environment for Ruby development.

  2. Get the Azure SQL Database connection configurations from the environment variables added by Service Connector.

    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

For other languages, use the connection properties that Service Connector sets to the environment variables to connect the database. For environment variable details, see Integrate Azure SQL Database with Service Connector.


For more information, see Homepage for client programming to Microsoft SQL Server.