private void ShowDataLinks(object sender, EventArgs 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, Properties.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, ParentForm.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 string newConnectionString = null; dataInitialize.GetInitializationString(dataSource, true, out newConnectionString); // Parse the new connection string into the connection properties object Properties.Parse(newConnectionString); // Reload the control with the modified connection properties LoadProperties(); } catch (Exception ex) { COMException comex = ex as COMException; if (comex == null || comex.ErrorCode != NativeMethods.DB_E_CANCELED) { IUIService uiService = this.GetService(typeof(IUIService)) as IUIService; if (uiService != null) { uiService.ShowError(ex); } else { RTLAwareMessageBox.Show(null, ex.Message, MessageBoxIcon.Exclamation); } } } }
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); } } }