Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
This set of articles explains how to work with Azure SQL bindings in Azure Functions. Azure Functions supports input bindings, output bindings, and a function trigger for the Azure SQL and SQL Server products.
| Action | Type |
|---|---|
| Trigger a function when a change is detected on a SQL table | SQL trigger |
| Read data from a database | Input binding |
| Save data to a database | Output binding |
Install extension
The extension NuGet package you install depends on the C# mode you're using in your function app:
Functions execute in an isolated C# worker process. To learn more, see Guide for running C# Azure Functions in an isolated worker process.
Add the extension to your project by installing this NuGet package.
dotnet add package Microsoft.Azure.Functions.Worker.Extensions.Sql
To use a preview version of the Microsoft.Azure.Functions.Worker.Extensions.Sql package, add the --prerelease flag to the command. You can view preview functionality on the Azure Functions SQL Extensions release page.
dotnet add package Microsoft.Azure.Functions.Worker.Extensions.Sql --prerelease
Note
Breaking changes between preview releases of the Azure SQL bindings for Azure Functions requires that all Functions targeting the same database use the same version of the SQL extension package.
Install bundle
To be able to use this binding extension in your app, make sure that the host.json file in the root of your project contains this extensionBundle reference:
{
"version": "2.0",
"extensionBundle": {
"id": "Microsoft.Azure.Functions.ExtensionBundle",
"version": "[4.0.0, 5.0.0)"
}
}
In this example, the version value of [4.0.0, 5.0.0) instructs the Functions host to use a bundle version that is at least 4.0.0 but less than 5.0.0, which includes all potential versions of 4.x. This notation effectively maintains your app on the latest available minor version of the v4.x extension bundle.
When possible, you should use the latest extension bundle major version and allow the runtime to automatically maintain the latest minor version. You can view the contents of the latest bundle on the extension bundles release page. For more information, see Azure Functions extension bundles.
If your app needs to use preview functionality, you should instead reference the latest version of the preview bundle. For more information, see Work with preview extension bundles.
You can view preview functionality on the Azure Functions SQL Extensions release page.
Note
Breaking changes between preview releases of the Azure SQL bindings for Azure Functions requires that all Functions targeting the same database use the same version of the SQL extension package.
Update packages
Add the Azure Functions Java SQL Types package to your functions project with an update to the pom.xml file in your project, as in this example:
<dependency>
<groupId>com.microsoft.azure.functions</groupId>
<artifactId>azure-functions-java-library-sql</artifactId>
<version>2.1.0</version>
</dependency>
Connections
The connectionStringSetting property is set to a key in application settings that returns a value used by the Functions runtime to connect to the Azure SQL or SQL Server database used by the extension. The value of the connection property setting depends on the type of connection:
- Managed identity connection: The
connectionStringSettingproperty returns a connection string that usesAuthentication=Active Directory Managed Identityto authenticate without secrets. You can use either a system-assigned or user-assigned managed identity. For more information, see Connect a function app to Azure SQL with managed identity and Define identity connections. - Key Vault reference: The
connectionStringSettingproperty setting returns an Azure Key Vault reference to the location where the connection string is centrally maintained. For more information, see Define Key Vault connections. - App Configuration reference: The
connectionStringSettingproperty setting returns an Azure App Configuration reference that returns a connection string or a Key Vault reference. For more information, see Azure App Configuration in the connections article. - Connection string: The
connectionStringSettingproperty setting returns the actual SQL connection string. Because the connection string may contain credentials, you should use a managed identity connection or at least store the connection string in Key Vault. For more information, see Define connections.
To learn more about bindings connections, see Manage connections in Azure Functions.
The connection string is passed to Microsoft.Data.SqlClient and supports all keywords defined in the SqlClient ConnectionString documentation. Notable keywords include:
Authentication: Connect to Azure SQL with Microsoft Entra ID. Set toActive Directory Managed Identityfor managed identities. For more information, see Connect a function app to Azure SQL with managed identity.Command Timeout: Wait for a specified amount of time in seconds before terminating a query (default 30 seconds).ConnectRetryCount: Automatically make additional reconnection attempts, especially applicable to Azure SQL Database serverless tier (default 1).Pooling: Reuse connections to the database to improve performance (defaulttrue). Additional settings for connection pooling includeConnection Lifetime,Max Pool Size, andMin Pool Size. Learn more in the ADO.NET documentation.
Considerations
- Azure SQL binding supports version 4.x and later of the Functions runtime.
- Source code for the Azure SQL bindings can be found in this GitHub repository.
- This binding requires connectivity to an Azure SQL or SQL Server database.
- Output bindings against tables with columns of data types
NTEXT,TEXT, orIMAGEaren't supported and data upserts will fail. These types will be removed in a future version of SQL Server and aren't compatible with theOPENJSONfunction used by this Azure Functions binding. - Use managed identities instead of usernames and passwords.
- Consider using an Azure Key Value to store application settings.
Samples
In addition to the samples for C#, Java, JavaScript, PowerShell, and Python available in the Azure SQL bindings GitHub repository, more are available in Azure Samples: