Поэтому я пытаюсь просто записать некоторые данные из функции Azure в базу данных.Мой код выглядит следующим образом
Мне удалось увидеть, что ошибка генерируется в cmd.ExecuteNonQuery (), потому что моя log.information показывает
2019-05-29T11:21:45.149 [Info] First Stop
2019-05-29T11:21:45.149 [Info] Connection Made
2019-05-29T11:21:45.149 [Info] Open Connection
2019-05-29T11:21:45.149 [Info] Create Query InsertInto
2019-05-29T11:21:45.149 [Info] enter SQL Command cmd
2019-05-29T11:21:45.149 [Info] insert a log to the database
2019-05-29T11:21:45.160 [Info] Function completed (Success, Id=7e507164-a17d-4aa7-84bd-279e0934514e, Duration=261ms)
using System.Net;
using Dapper;
using System.Data.SqlClient;
using System.Configuration;
[FunctionName("CreateLoggsToDbWithAzureFunctions")]
public static async Task<HttpResponseMessage> Run(HttpRequestMessage req, ILogger log)
{
log.LogInformation($"C# HTTP trigger function processed a request. RequestUri={req.RequestUri}");
var successful =true;
try
{
log.LogInformation("First Stop");
var cnnString = ConfigurationManager.ConnectionStrings["sqldb_connection"].ConnectionString;
//Environment.GetEnvironmentVariable("sqldb_connection");
log.LogInformation("Connection Made", cnnString);
using(var connection = new SqlConnection(cnnString))
{
connection.Open();
log.LogInformation("Open Connection");
//var rLog = await req.Content.ReadAsAsync<LogRequest>();
log.LogInformation("Create Query InsertInto");
var query = "INSERT INTO Loggs (LogError)";
query += "VALUES (@LogError)";
log.LogInformation("enter SQL Command cmd");
using(SqlCommand cmd = new SqlCommand(query, connection))
{
log.LogInformation("insert a log to the database");
var rows = cmd.ExecuteNonQuery();
log.LogInformation("Log added to database successfully!");
}
//connection.Execute(query, rLog);
// insert a log to the database
// NOTE: Execute is an extension method from Dapper library
//connection.Execute("INSERT INTO [dbo].[Loggs] ([LogError]) VALUES (@LogError)", rLog);
//log.LogInformation("Log added to database successfully!");
}
}
catch
{
successful=false;
}
return !successful
? req.CreateResponse(HttpStatusCode.BadRequest, "Unable to process your request!")
: req.CreateResponse(HttpStatusCode.OK, "Data saved successfully!");
}
public class LogRequest
{
//public string Id{get;set;}
//public string Date{get;set;}
//public string LogCritical{get;set;}
//public string LogDebug{get;set;}
public string LogError{get;set;}
//public string LogInformation{get;set;}
//public string LogTrace{get;set;}
//public string LogWarning{get;set;}
}
Я пробовал разные методыкак использование dapper и создание connection.Execute (query, rLog);или делает
connection.Execute("INSERT INTO [dbo].[Loggs] ([LogError]) VALUES (@LogError)", rLog);`
Но в базе данных ничего не сохраняется, и я получаю сообщение
2019-05-29T11:21:45.160 [Info] Function completed
Но ничего не происходит, у кого-нибудь есть идея?