static void Main(string[] args) { BaseDBConnection msConnection = new MSSQLService("select * from somePostgreeTable"); using (msConnection) { msConnection.RunQuery(); } Console.WriteLine(); BaseDBConnection pgConnection = new PostgreeSQLService(); pgConnection.Query = "select * from somePostgreeTable"; using (pgConnection) { pgConnection.RunQuery(); } }
public static async Task <IActionResult> Run( [HttpTrigger(AuthorizationLevel.Function, "post", Route = null)] HttpRequest request, ILogger log) { log.LogInformation("C# HTTP trigger function [SimpleLog] processed a request."); string connectionStringMSSQL = Environment.GetEnvironmentVariable("ConnectionStringMSQSQL.SimpleLog.HttpTrigger"); string connectionStringMongoDB = Environment.GetEnvironmentVariable("ConnectionStringMongoDB.SimpleLog.HttpTrigger"); ; var toSaveMSSQL = !string.IsNullOrEmpty(connectionStringMSSQL); var toSaveMongoDB = !string.IsNullOrEmpty(connectionStringMongoDB); if (!toSaveMSSQL && !toSaveMongoDB) { var errorMessageConnectionString = "the connection string is empty."; log.LogError(errorMessageConnectionString); return(new ObjectResult(new { Error = errorMessageConnectionString }) { StatusCode = (int)HttpStatusCode.InternalServerError }); } string requestBody = await new StreamReader(request.Body).ReadToEndAsync(); var viewModel = JsonConvert.DeserializeObject <SimpleLogViewModel>(requestBody); var entity = new SimpleLogEntity(viewModel.OurClientId, viewModel.ApplicationId, viewModel.LogLevel, viewModel.Description, viewModel.Json); if (entity.HasNotification()) { return(new BadRequestObjectResult(entity.GetNotification())); } var mssql = new MSSQLService(connectionStringMSSQL); var mongoDB = new MongoDBService(connectionStringMongoDB); if (!(request.Headers["X-Sync-Request"].ToString() == "true")) { if (toSaveMSSQL) { _ = mssql.SaveAsync(log, entity); } if (toSaveMongoDB) { _ = mongoDB.SaveAsync(log, entity); } return(new CreatedResult( GetResourceUrl(request, entity.Id), new { Message = "Event sent.", ResourceId = entity.Id })); } var savedMSSQL = false; if (toSaveMSSQL) { savedMSSQL = await mssql.SaveAsync(log, entity); } var savedMongoDB = false; if (toSaveMongoDB) { savedMongoDB = await mongoDB.SaveAsync(log, entity); } var conditionBoth = ((toSaveMSSQL && savedMSSQL) && (toSaveMongoDB && savedMongoDB)); var conditionMSQSQL = !toSaveMongoDB && (toSaveMSSQL && savedMSSQL); var conditionMongoDB = !toSaveMSSQL && (toSaveMongoDB && savedMongoDB); if (conditionBoth || conditionMSQSQL || conditionMongoDB) { return(new CreatedResult( GetResourceUrl(request, entity.Id), new { Message = "The object was saved in the database.", ResourceId = entity.Id })); } else { return(new UnprocessableEntityObjectResult(new { Error = $"The object could not be saved to the database." })); } }