forked from jquintus/spikes
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAfSqlQuery
47 lines (41 loc) · 1.64 KB
/
AfSqlQuery
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
using System.IO;
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;
using System.Collections.Generic;
public static class SqlDataFunction
{
[FunctionName("GetDataFromSql")]
public static IActionResult Run(
[HttpTrigger(AuthorizationLevel.Function, "get", Route = null)] HttpRequest req,
ILogger log)
{
log.LogInformation("C# HTTP trigger function processed a request.");
string connectionString = "YourOnPremiseConnectionStringHere"; // replace with your on-premise SQL Server connection string
string query = "SELECT * FROM YourTableName"; // replace with your desired SQL query
List<object> results = new List<object>();
using (SqlConnection connection = new SqlConnection(connectionString))
{
connection.Open();
using (SqlCommand cmd = new SqlCommand(query, connection))
{
using (SqlDataReader reader = cmd.ExecuteReader())
{
while (reader.Read())
{
// For simplicity, just adding the entire row as an object.
// You might want to map this to a specific class or structure.
var record = new object[reader.FieldCount];
reader.GetValues(record);
results.Add(record);
}
}
}
}
return new OkObjectResult(results);
}
}