public DTOStatus ConnectionSetting([FromBody] DTOConnectionSetting objConnectionSetting) { // The return message DTOStatus objDTOStatus = new DTOStatus(); objDTOStatus.Success = true; // Do not run if we can connect to the current database if (CurrentVersion().isNewDatabase == false) { objDTOStatus.Success = false; objDTOStatus.StatusMessage = "Database already set-up"; } else { // Create a Database connection string string strConnectionString = CreateDatabaseConnectionString(objConnectionSetting); // Test the database connection string if (DatabaseConnectionValid(strConnectionString)) { try { // Update the appsettings.json file UpdateDatabaseConnectionString(strConnectionString); // Update the in-memory connection string _DefaultConnection = strConnectionString; } catch (Exception ex) { // appsettings.json file update error objDTOStatus.Success = false; objDTOStatus.StatusMessage = ex.GetBaseException().Message; } } else { // Bad connection setting objDTOStatus.Success = false; objDTOStatus.StatusMessage = "Connection settings are not valid"; } } // Return the result return(objDTOStatus); }
private string CreateDatabaseConnectionString(DTOConnectionSetting objConnectionSetting) { StringBuilder SB = new StringBuilder(); string strConnectionString = ""; string strUserInfo = (!objConnectionSetting.IntegratedSecurity) ? String.Format("uid={0};pwd={1}", objConnectionSetting.Username, objConnectionSetting.Password) : "integrated security=True"; strConnectionString = String.Format("{0}data source={1};initial catalog={2};{3}", SB.ToString(), objConnectionSetting.ServerName, objConnectionSetting.DatabaseName, strUserInfo); return(strConnectionString); }