#region License // Copyright 2005-2019 Paul Kohler (https://github.com/paulkohler/minisqlquery). All rights reserved. // This source code is made available under the terms of the GNU Lesser General Public License v3.0 // https://github.com/paulkohler/minisqlquery/blob/master/LICENSE #endregion using System; using System.Data.Common; using System.Windows.Forms; using MiniSqlQuery.Core; using MiniSqlQuery.Properties; namespace MiniSqlQuery.PlugIns.ConnectionStringsManager { /// The connection string builder form. public partial class ConnectionStringBuilderForm : Form { /// The _host window. private readonly IHostWindow _hostWindow; /// The _services. private readonly IApplicationServices _services; /// Gets or sets ConnectionDefinition. public DbConnectionDefinition ConnectionDefinition { get; set; } /// The default provider name. public const string DefaultProviderName = "System.Data.SqlClient"; /// The _initialised. private bool _initialised; /// The _loaded. private bool _loaded; /// The _init provider. private string _initProvider; /// The _provider name. private string _providerName = DefaultProviderName; /// The _conn str. private string _connStr; /// The _conn str bldr. private DbConnectionStringBuilder _connStrBldr; /// The _dirty. private bool _dirty; /// Gets or sets ConnectionName. public string ConnectionName { get { return txtConnectionName.Text; } set { txtConnectionName.Text = value; } } /// Gets or sets Comments. public string Comments { get { return txtComments.Text; } set { txtComments.Text = value; } } /// Gets or sets ProviderName. public string ProviderName { get { return _providerName; } set { _providerName = value; cboProvider.SelectedItem = _providerName; } } /// /// The supplied connection string - or if during an edit - the new one. /// public string ConnectionString { get { if (_connStrBldr != null) { return _connStrBldr.ConnectionString; } return _connStr; } set { _connStr = value; // store if (_connStrBldr == null) { BindNewConnectionStringBuilder(); } } } /// Gets DbConnectionStringBuilderInstance. public DbConnectionStringBuilder DbConnectionStringBuilderInstance { get { return _connStrBldr; } } /// Initializes a new instance of the class. /// The host window. /// The services. public ConnectionStringBuilderForm(IHostWindow hostWindow, IApplicationServices services) { InitializeComponent(); _hostWindow = hostWindow; _services = services; Icon = ImageResource.database_edit_icon; } /// Initializes a new instance of the class. /// The host window. /// The definition. /// The services. public ConnectionStringBuilderForm(IHostWindow hostWindow, DbConnectionDefinition definition, IApplicationServices services) : this(hostWindow, services) { ConnectionDefinition = definition; ConnectionName = ConnectionDefinition.Name; Comments = ConnectionDefinition.Comment; _initProvider = ConnectionDefinition.ProviderName; _connStr = ConnectionDefinition.ConnectionString; } /// The connection string builder form_ load. /// The sender. /// The e. private void ConnectionStringBuilderForm_Load(object sender, EventArgs e) { cboProvider.DataSource = Utility.GetSqlProviderNames(); _initialised = true; } /// The connection string builder form_ shown. /// The sender. /// The e. private void ConnectionStringBuilderForm_Shown(object sender, EventArgs e) { if (_initProvider == null) { ProviderName = DefaultProviderName; } else { ProviderName = _initProvider; } _loaded = true; } /// The connection string builder form_ form closing. /// The sender. /// The e. private void ConnectionStringBuilderForm_FormClosing(object sender, FormClosingEventArgs e) { if (_dirty) { DialogResult saveFile = _hostWindow.DisplayMessageBox( this, Resources.The_connection_details_have_changed__do_you_want_to_save, Resources.Save_Changes, MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question, MessageBoxDefaultButton.Button1, 0, null, null); switch (saveFile) { case DialogResult.Yes: WriteValuesBack(); break; case DialogResult.Cancel: e.Cancel = true; break; } } } /// The tool strip button ok_ click. /// The sender. /// The e. private void toolStripButtonOk_Click(object sender, EventArgs e) { DialogResult = DialogResult.OK; WriteValuesBack(); Close(); } /// The tool strip button cancel_ click. /// The sender. /// The e. private void toolStripButtonCancel_Click(object sender, EventArgs e) { // todo - is dirty? DialogResult = DialogResult.Cancel; Close(); } /// The write values back. protected void WriteValuesBack() { if (ConnectionDefinition == null) { ConnectionDefinition = new DbConnectionDefinition(); } ConnectionDefinition.Name = ConnectionName; ConnectionDefinition.ProviderName = ProviderName; ConnectionDefinition.ConnectionString = ConnectionString; ConnectionDefinition.Comment = Comments; _dirty = false; } /// The bind new connection string builder. private void BindNewConnectionStringBuilder() { if (!_initialised) { return; } bool getBuilderFailed = false; try { _connStrBldr = DbProviderFactories.GetFactory(ProviderName).CreateConnectionStringBuilder(); if (_connStrBldr == null) { getBuilderFailed = true; } } catch { getBuilderFailed = true; } if (getBuilderFailed) { _connStrBldr = new GenericConnectionStringBuilder(); } try { _connStrBldr.ConnectionString = _connStr; } catch (ArgumentException argExp) { // consume error but notify MessageBox.Show(Resources.BindNewConnectionStringBuilder_Could_not_populate_with_current_connection_string___ + argExp.Message); } propertyGridDbConnection.SelectedObject = _connStrBldr; } /// The cbo provider_ selected index changed. /// The sender. /// The e. private void cboProvider_SelectedIndexChanged(object sender, EventArgs e) { if (!_initialised) { return; } SetDirty(); if (cboProvider.SelectedItem != null) { ProviderName = cboProvider.SelectedItem.ToString(); BindNewConnectionStringBuilder(); } } /// The items text changed. /// The sender. /// The e. private void ItemsTextChanged(object sender, EventArgs e) { SetDirty(); } /// The property grid db connection_ property value changed. /// The s. /// The e. private void propertyGridDbConnection_PropertyValueChanged(object s, PropertyValueChangedEventArgs e) { SetDirty(); } /// The set dirty. private void SetDirty() { if (!_loaded) { return; } if (!_dirty) { _dirty = true; Text += "*"; } } /// The tool strip button test_ click. /// The sender. /// The e. private void toolStripButtonTest_Click(object sender, EventArgs e) { // do a standalone raw connection test Exception exp = QueryRunner.TestDbConnection(ProviderName, ConnectionString); if (exp == null) { string msg = string.Format(Resources.Connected_to_0_successfully, ConnectionName); _hostWindow.DisplaySimpleMessageBox(this, msg, Resources.Connection_Successful); } else { string msg = string.Format(Resources.Failed_connecting_to_0_1_2, ConnectionName, Environment.NewLine, exp.Message); _hostWindow.DisplaySimpleMessageBox(this, msg, Resources.Connection_Failed); } } #if DEBUG /// The to string. /// The to string. public override string ToString() { return string.Format("[ConnectionStringBuilderForm => Name: {0}; Provider: {1}; ConnectionString: {2}]", ConnectionName, ProviderName, ConnectionString); } #endif } }