private SqlConnection CreateSQLConnection(SQLConnectionSettings connectionSettings) { var sb = new System.Data.SqlClient.SqlConnectionStringBuilder(instance.connectionString) { DataSource = txtDBServer.Text }; if (connectionSettings == SQLConnectionSettings.Database) { sb.InitialCatalog = txtDBName.Text; } else { sb.InitialCatalog = ""; } sb.IntegratedSecurity = chkIntegratedSecurity.Checked; if (!chkIntegratedSecurity.Checked) { sb.UserID = txtDBUser.Text; sb.Password = txtDBPass.Text; } System.Data.SqlClient.SqlConnection sqlConn = new System.Data.SqlClient.SqlConnection(sb.ConnectionString); if (connectionSettings == SQLConnectionSettings.Database) { sqlConn.InfoMessage += SqlConn_InfoMessage; sqlConn.StateChange += SqlConnDB_StateChange; } else { sqlConn.StateChange += SqlConn_StateChange; sqlConn.InfoMessage += SqlConn_InfoMessage; } return(sqlConn); }
// This method gets called by the runtime. Use this method to add services to the container public void ConfigureServices(IServiceCollection services) { services.AddMvc(); var sqlConnectionSettingsString = Environment.GetEnvironmentVariable("SQLConnectionSettings"); var sqlConnectionSettings = new SQLConnectionSettings(); if (!string.IsNullOrEmpty(sqlConnectionSettingsString)) { sqlConnectionSettings = JsonConvert.DeserializeObject <SQLConnectionSettings>(sqlConnectionSettingsString); } services.AddSingleton <ISQLConnectionSettings>(sqlConnectionSettings); services.AddTransient <IWidgetRepository, WidgetRepository>(); }
private async Task TestSQLConnectionAsync(SQLConnectionSettings connectionSettings) { try { sqlConn = CreateSQLConnection(connectionSettings); if (SQLTask != null && !SQLTask.IsCompleted) { try { cts.Cancel(); while (!SQLTask.IsCanceled && !SQLTask.IsCompleted) { Application.DoEvents(); } SQLTask.Dispose(); } catch { } } cts = new CancellationTokenSource(); SQLTask = sqlConn.OpenAsync(cts.Token); await SQLTask; } catch (TaskCanceledException ex) { System.Diagnostics.Debug.WriteLine(ex.Message); } catch (Exception ex) { if (connectionSettings == SQLConnectionSettings.Database) { chkNewDB.Checked = true; } else { System.Diagnostics.Debug.Print("TestSQLConnectionAsync:{0}", ex.Message); picStatus.Image = Properties.Resources.Err; } } }
/// <summary> /// Get the database connection settings from the SQL plugin, e.g. for /// use in setting up an auxiliary database connection. /// /// Throws an exception if the settings could not be found. /// </summary> /// <returns>The database connection settings are returned.</returns> public static SQLConnectionSettings GetSQLConnectionSettings() { SQLConnectionSettings Settings = new SQLConnectionSettings(); string[] Lines = File.ReadAllLines(GetNWNX4InstallationDirectory() + "xp_mysql.ini"); string[] Keywords = { "server", "user", "password", "schema" }; foreach (string Line in Lines) { foreach (string Keyword in Keywords) { if (!Line.StartsWith(Keyword)) { continue; } // // Skip to the equals sign and check that the intervening // characters were all whitespace. // int Equals = Line.IndexOf('='); if (Equals == -1 || Equals < Keyword.Length) { continue; } for (int i = Keyword.Length; i < Equals; i += 1) { if (!Char.IsWhiteSpace(Line[i])) { Equals = -1; break; } } if (Equals == -1) { continue; } // // Now get the value and set it in the structure. // string Value = Line.Substring(Equals + 1).TrimStart(new char[] { '\t', ' ' }); FieldInfo Field = typeof(SQLConnectionSettings).GetField(Keyword, BindingFlags.Public | BindingFlags.IgnoreCase | BindingFlags.Instance); Field.SetValue(Settings, Value); } } if (String.IsNullOrWhiteSpace(Settings.Server)) { throw new ApplicationException("No database server specified in the configuraiton file."); } if (String.IsNullOrWhiteSpace(Settings.User)) { throw new ApplicationException("No database user specified in the configuration file."); } if (String.IsNullOrWhiteSpace(Settings.Password)) { throw new ApplicationException("No database password specified in the configuration file."); } if (String.IsNullOrWhiteSpace(Settings.Schema)) { throw new ApplicationException("No database schema specified in the configuration file."); } return(Settings); }
/// <summary> /// Get the database connection settings from the SQL plugin, e.g. for /// use in setting up an auxiliary database connection. /// /// Throws an exception if the settings could not be found. /// </summary> /// <returns>The database connection settings are returned.</returns> public static SQLConnectionSettings GetSQLConnectionSettings() { SQLConnectionSettings Settings = new SQLConnectionSettings(); string[] Lines = File.ReadAllLines(GetNWNX4InstallationDirectory() + "xp_mysql.ini"); string[] Keywords = {"server", "user", "password", "schema"}; foreach (string Line in Lines) { foreach (string Keyword in Keywords) { if (!Line.StartsWith(Keyword)) continue; // // Skip to the equals sign and check that the intervening // characters were all whitespace. // int Equals = Line.IndexOf('='); if (Equals == -1 || Equals < Keyword.Length) continue; for (int i = Keyword.Length; i < Equals; i += 1) { if (!Char.IsWhiteSpace(Line[i])) { Equals = -1; break; } } if (Equals == -1) continue; // // Now get the value and set it in the structure. // string Value = Line.Substring(Equals + 1).TrimStart(new char[] { '\t', ' '}); FieldInfo Field = typeof(SQLConnectionSettings).GetField(Keyword, BindingFlags.Public | BindingFlags.IgnoreCase | BindingFlags.Instance); Field.SetValue(Settings, Value); } } if (String.IsNullOrWhiteSpace(Settings.Server)) throw new ApplicationException("No database server specified in the configuraiton file."); if (String.IsNullOrWhiteSpace(Settings.User)) throw new ApplicationException("No database user specified in the configuration file."); if (String.IsNullOrWhiteSpace(Settings.Password)) throw new ApplicationException("No database password specified in the configuration file."); if (String.IsNullOrWhiteSpace(Settings.Schema)) throw new ApplicationException("No database schema specified in the configuration file."); return Settings; }