private void Build_Click(object sender, RoutedEventArgs e)
        {
            IntPtr henv   = IntPtr.Zero;
            IntPtr hdbc   = IntPtr.Zero;
            short  result = 0;

            try
            {
                result = NativeMethods.SQLAllocEnv(out henv);
                if (!NativeMethods.SQL_SUCCEEDED(result))
                {
                    throw new ApplicationException(Properties.Resources.OdbcConnectionUIControl_SQLAllocEnvFailed);
                }

                result = NativeMethods.SQLAllocConnect(henv, out hdbc);
                if (!NativeMethods.SQL_SUCCEEDED(result))
                {
                    throw new ApplicationException(Properties.Resources.OdbcConnectionUIControl_SQLAllocConnectFailed);
                }

                string currentConnectionString = _connectionProperties.ToFullString();
                System.Text.StringBuilder newConnectionString = new System.Text.StringBuilder(1024);
                result = NativeMethods.SQLDriverConnect(hdbc, new WindowInteropHelper(Window.GetWindow(this)).Handle, currentConnectionString, (short)currentConnectionString.Length, newConnectionString, 1024, out short newConnectionStringLength, NativeMethods.SQL_DRIVER_PROMPT);
                if (!NativeMethods.SQL_SUCCEEDED(result) && result != NativeMethods.SQL_NO_DATA)
                {
                    // Try again without the current connection string, in case it was invalid
                    result = NativeMethods.SQLDriverConnect(hdbc, new WindowInteropHelper(Window.GetWindow(this)).Handle, null, 0, newConnectionString, 1024, out newConnectionStringLength, NativeMethods.SQL_DRIVER_PROMPT);
                }
                if (!NativeMethods.SQL_SUCCEEDED(result) && result != NativeMethods.SQL_NO_DATA)
                {
                    throw new ApplicationException(Properties.Resources.OdbcConnectionUIControl_SQLDriverConnectFailed);
                }
                else
                {
                    NativeMethods.SQLDisconnect(hdbc);
                }

                if (newConnectionStringLength > 0)
                {
                    Refresh_Click(sender, e);
                    _connectionProperties.Parse(newConnectionString.ToString());
                    VisualTreeHelpers.RefreshBindings(this);
                    passwordTextbox.Password = Password;
                }
            }
            finally
            {
                if (hdbc != IntPtr.Zero)
                {
                    NativeMethods.SQLFreeConnect(hdbc);
                }
                if (henv != IntPtr.Zero)
                {
                    NativeMethods.SQLFreeEnv(henv);
                }
            }
        }
        private void AdvancedButton_Click(object sender, RoutedEventArgs e)
        {
            DataConnectionAdvancedDialog dataAdvancedDialog = new DataConnectionAdvancedDialog(ConnectionProperties);

            if (dataAdvancedDialog.ShowOkCancel())
            {
                VisualTreeHelpers.RefreshBindings(this);
            }
        }
        private void ProviderCombobox_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            locationLabel.IsEnabled            = false;
            locationTextBox.IsEnabled          = false;
            sqlAuthentication.IsChecked        = true;
            integraredSecRadioButton.IsEnabled = false;
            sqlAuthentication.IsEnabled        = false;
            usernameTextbox.IsEnabled          = false;
            usernameLabel.IsEnabled            = false;
            passwordLabel.IsEnabled            = false;
            passwordTextbox.IsEnabled          = false;
            savepasswordCheckbox.IsEnabled     = false;
            initialCatalogGroup.IsEnabled      = false;
            sqlAuthentication.IsChecked        = true;

            PropertyDescriptorCollection propertyDescriptors = TypeDescriptor.GetProperties(_connectionProperties);
            PropertyDescriptor           propertyDescriptor  = null;

            if ((propertyDescriptor = propertyDescriptors["Location"]) != null &&
                propertyDescriptor.IsBrowsable)
            {
                locationLabel.IsEnabled   = true;
                locationTextBox.IsEnabled = true;
            }
            if ((propertyDescriptor = propertyDescriptors["Integrated Security"]) != null &&
                propertyDescriptor.IsBrowsable)
            {
                integraredSecRadioButton.IsEnabled = true;
            }
            if ((propertyDescriptor = propertyDescriptors["User ID"]) != null &&
                propertyDescriptor.IsBrowsable)
            {
                usernameTextbox.IsEnabled   = true;
                usernameLabel.IsEnabled     = true;
                sqlAuthentication.IsEnabled = true;
            }
            if (_connectionProperties["Password"] != null)
            {
                passwordLabel.IsEnabled     = true;
                passwordTextbox.IsEnabled   = true;
                sqlAuthentication.IsEnabled = true;
            }
            if (_connectionProperties["Password"] != null &&
                (propertyDescriptor = propertyDescriptors["PersistSecurityInfo"]) != null &&
                propertyDescriptor.IsBrowsable)
            {
                savepasswordCheckbox.IsEnabled = true;
            }
            if ((propertyDescriptor = propertyDescriptors["Initial Catalog"]) != null &&
                propertyDescriptor.IsBrowsable)
            {
                initialCatalogGroup.IsEnabled = true;
            }
            VisualTreeHelpers.RefreshBindings(this);
        }
 private void ConStrTextBox_LostFocus(object sender, RoutedEventArgs e)
 {
     try
     {
         _connectionProperties.Parse(conStrTextBox.Text.Trim());
         VisualTreeHelpers.RefreshBindings(this);
         passwordTextbox.Password = Password;
     }
     catch (ArgumentException ex)
     {
         MessageBox.Show(ex.Message, Properties.Resources.Error_Label, MessageBoxButton.OK, MessageBoxImage.Error);
         UpdateConnectionString();
     }
 }
        private void DataLink_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                // Create data links object as IDataInitialize
                Type dataLinksType = Type.GetTypeFromCLSID(NativeMethods.CLSID_DataLinks);
                NativeMethods.IDataInitialize dataInitialize = Activator.CreateInstance(dataLinksType) as NativeMethods.IDataInitialize;

                // Create data source object from connection string
                object dataSource = null;
                dataInitialize.GetDataSource(null,
                                             NativeMethods.CLSCTX_INPROC_SERVER,
                                             _connectionProperties.ToFullString(),
                                             ref NativeMethods.IID_IUnknown,
                                             ref dataSource);

                // Get IDBPromptInitialize interface from data links object
                NativeMethods.IDBPromptInitialize promptInitialize = (NativeMethods.IDBPromptInitialize)dataInitialize;

                // Display the data links dialog using this data source
                promptInitialize.PromptDataSource(
                    null,
                    new WindowInteropHelper(Window.GetWindow(this)).Handle,
                    NativeMethods.DBPROMPTOPTIONS_PROPERTYSHEET | NativeMethods.DBPROMPTOPTIONS_DISABLE_PROVIDER_SELECTION,
                    0,
                    IntPtr.Zero,
                    null,
                    ref NativeMethods.IID_IUnknown,
                    ref dataSource);

                // Retrieve the new connection string from the data source
                dataInitialize.GetInitializationString(dataSource, true, out string newConnectionString);

                // Parse the new connection string into the connection properties object
                _connectionProperties.Parse(newConnectionString);

                // Reload the control with the modified connection properties
                VisualTreeHelpers.RefreshBindings(this);
                passwordTextbox.Password = Password;
            }
            catch (Exception ex)
            {
                COMException comex = ex as COMException;
                if (comex == null || comex.ErrorCode != NativeMethods.DB_E_CANCELED)
                {
                    MessageBox.Show(ex.Message, Properties.Resources.Error_Label, MessageBoxButton.OK, MessageBoxImage.Error);
                }
            }
        }