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);
                }
            }
        }
Exemplo n.º 2
0
        public DataConnectionAdvancedDialog(IDataConnectionProperties connectionProperties, DataConnectionDialog mainDialog)
            : this()
        {
            Debug.Assert(connectionProperties != null);
            Debug.Assert(mainDialog != null);

            _savedConnectionString = connectionProperties.ToFullString();

            this.propertyGrid.SelectedObject = connectionProperties;

            _mainDialog = mainDialog;
        }
        public DataConnectionAdvancedDialog(IDataConnectionProperties connectionProperties, DataConnectionDialog mainDialog)
            : this()
        {
            Debug.Assert(connectionProperties != null);
            Debug.Assert(mainDialog != null);

            _savedConnectionString = connectionProperties.ToFullString();

            this.propertyGrid.SelectedObject = connectionProperties;

            _mainDialog = mainDialog;
        }