メニューにある ADO.NET の値 例: 新規 ToDo を書き込む API (c) 2022 hajimes.life var conn_str = “接続⽂字列の値”; using (SqlConnection conn = new SqlConnection(conn_str)) { conn.Open(); var text = ”SQL ⽂"; using (SqlCommand cmd = new SqlCommand(text, conn)) { await cmd.ExecuteNonQueryAsync(); } } SQL DB へ接続するコード例 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.Extensions.Configuration; using System.Data.SqlClient; namespace TodoApi; public static class AddTodo { [FunctionName("AddTodo")] public static async Task<IActionResult> Run( [HttpTrigger(AuthorizationLevel.Function, "post", Route=null)] HttpRequest req, ILogger log) { string requestBody = await new StreamReader(req.Body).ReadToEndAsync(); dynamic data = JsonConvert.DeserializeObject(requestBody); string description = data?.description; DateTime createdDate = DateTime.Now; using (var conn = new SqlConnection(“接続⽂字列の値”)) { conn.Open(); using (var cmd = conn.CreateCommand()) { cmd.CommandText = "INSERT INTO todos(description, createdDate) VALUES(@description, @createdDate)"; cmd.Parameters.AddWithValue("@description", description); cmd.Parameters.AddWithValue("@createdDate", createdDate); await cmd.ExecuteNonQueryAsync(); } } return new OkObjectResult("ok"); } }