-
Notifications
You must be signed in to change notification settings - Fork 0
/
HttpTriggerSqlClient.cs
49 lines (46 loc) · 1.79 KB
/
HttpTriggerSqlClient.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
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 Microsoft.Data.SqlClient;
namespace SqlClientTestFunction
{
public static class HttpTriggerSqlClient
{
[FunctionName("HttpTriggerSqlClient")]
public static async Task<IActionResult> Run(
[HttpTrigger(AuthorizationLevel.Function, "get", "post", Route = null)] HttpRequest req,
ILogger log)
{
log.LogInformation("C# HTTP trigger function processed a request.");
string serverName = req.Query["server"];
string uid = req.Query["uid"];
string pwd = req.Query["pwd"];
string cs = $"Server=tcp:{serverName}.database.windows.net,1433;Initial Catalog=NorthWind;User ID={uid};Password={pwd};";
string message = "Sorry!";
using (SqlConnection con = new SqlConnection(cs))
{
try
{
con.Open();
message = "You are connected!";
}
catch (SqlException e)
{
message = "An error occured: " + e.Message;
}
}
string requestBody = await new StreamReader(req.Body).ReadToEndAsync();
dynamic data = JsonConvert.DeserializeObject(requestBody);
serverName = serverName ?? data?.name;
return serverName != null
? (ActionResult)new OkObjectResult($"Hello, {uid}. {message}")
: new BadRequestObjectResult("Please pass a name on the query string or in the request body");
}
}
}