Create a gist now

Instantly share code, notes, and snippets.

Save JSON data into SQL Azure using Azure Functions
#r "Newtonsoft.Json"
using System;
using System.Net;
using System.Net.Mail;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using Dapper;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
public static async Task<HttpResponseMessage> Run(HttpRequestMessage req, TraceWriter log)
{
JObject contactFormData = await req.Content.ReadAsAsync<JObject>();
if (string.IsNullOrEmpty(contactFormData["EmailAddress"]?.ToString()))
return req.CreateResponse(HttpStatusCode.BadRequest, "Content is not in the expected format.");
string azureSQLConnectionString = ConfigurationManager.ConnectionStrings["SQLAzureTest"]?.ConnectionString;
string payload = JsonConvert.SerializeObject(contactFormData);
using(SqlConnection conn = new SqlConnection(azureSQLConnectionString))
{
conn.Execute("dbo.InsertContactForm", new { payload = payload }, commandType: CommandType.StoredProcedure);
}
return req.CreateResponse(HttpStatusCode.OK);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment