Azure / Azure/azure-functions-host

Analyzer: Suggested SqlConnection Use Pattern

Open
#7,416 0 comments 0 reactions 0 assignees View on GitHub
design Needs: Triage (Functions) supportability supportability: analyzer
Dominant language
C#
Stars
2k
Forks
482
Avg merge
2d 12h
Merged PRs (30d)
38

Description

**Conditions to Trigger Analzyer**
Within a `[Function]`-decorated method, where a SqlConnection is instantiated..

```csharp
using System;
using System.IO;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Azure.WebJobs;
using Microsoft.Azure.WebJobs.Extensions.Http;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Logging;
using Newtonsoft.Json;
using System.Data.SqlClient;

namespace TestManageConnectionApp
{
public static class SQLHTTPTrigger
{
[FunctionName("SQLHTTPTrigger")]
public static async Task Run(
[HttpTrigger(AuthorizationLevel.Function, "get", "post", Route = null)] HttpRequest req,
ILogger log)
{
log.LogInformation("C# HTTP trigger function processed a request.");

string connetionString;
SqlConnection cnn;
connetionString = @"Data Source=WIN-50GP30FGO75;Initial Catalog=Demodb;User ID=sa;Password=demol23";
cnn = new SqlConnection(connetionString);
cnn.Open();

string responseMessage = "This HTTP triggered function executed successfully.";

cnn.Close();
return new OkObjectResult(responseMessage);
}
}
}
```

**Diagnostic Level**
Warning

**How to Fix Violations**
Wrap SqlConnection instantiation in `using` statement, ex. https://github.com/cheenamalhotra/azurefunctions-sqlclient-example/blob/master/HttpTriggerSqlClient.cs

**Provide Code Fix**
Yes

**When to Suppress Analyzer Rule**
This rule should not be suppressed.

Contributor guide

Open the contributing guide

Research direction

Start with the trigger conditions and code example in the issue, then locate the repository's analyzer and code-fix entry points and their existing tests. Done means the analyzer warns for the described SqlConnection pattern inside a [Function]-decorated method and offers the requested using-statement fix.

Written by the indexing model from the issue text.

Assessment

Tech stack
csharp
Domain
devtools
Issue type
Feature
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.