I'm trying to create a simple CRUD to sql azure , but the example in azure site its reference to:
https://github.com/MicrosoftDocs/azure-docs/blob/master/articles/azure-functions/functions-scenario-database-table-cleanup.md
Anyone can help me with that, my first action it is a simple insert to database, but don't works.
public static async Task<IActionResult> Run(HttpRequest req, ILogger log)
{
log.LogInformation("C# HTTP trigger function processed a request.");
//log.Info($"ALFLOG======> RequestUri={req.RequestUri}");
string name = req.Query["name"];
string requestBody = await new StreamReader(req.Body).ReadToEndAsync();
dynamic data = JsonConvert.DeserializeObject(requestBody);
name = name ?? data?.name;
//--
var str = Environment.GetEnvironmentVariable("sqlconnec");
using (SqlConnection conn = new SqlConnection(str))
{
conn.Open();
var text = "insert into logs values ('demo2')";
using (SqlCommand cmd = new SqlCommand(text, conn))
{
// Execute the command and log the # rows affected.
var rows = await cmd.ExecuteNonQueryAsync();
log.LogInformation($"{rows} rows were updated");
}
}
//--
return name != null
? (ActionResult)new OkObjectResult($"Hello, {name}")
: new BadRequestObjectResult("Please pass a name on the query string or in the request body");
//+++We retrieve the userName field, which comes as a parameter to the function, by deserializing req.Content.
}
PD: my connection strings is "sqlconnec"
Thanks all!.
Alfredo