Last active
July 6, 2018 12:11
-
-
Save yorek/89fb1afecc34db3f9419ceb3ae9664d9 to your computer and use it in GitHub Desktop.
Save JSON data into SQL Azure using Azure Functions
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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