Пример #1
0
        /// <summary>
        /// Opens an <see cref="EditConnectionInfo"/> for a MySQL table.
        /// </summary>
        /// <param name="tableObject">Table to start an editing for.</param>
        /// <param name="fromSavedConnectionInfo">Flag indicating whether the <see cref="EditConnectionInfo"/> to be opened corresponds.</param>
        /// <param name="workbook">The workbook.</param>
        /// <returns><c>true</c> If the export/append action was executed, <c>false</c> otherwise.</returns>
        public bool EditTableData(DbTable tableObject, bool fromSavedConnectionInfo, ExcelInterop.Workbook workbook)
        {
            if (tableObject == null)
            {
                return(false);
            }

            var schemaAndTableNames = WbConnection.Schema + "." + tableObject.Name;

            // Check if the current DB object has an edit ongoing
            if (TableHasEditOnGoing(tableObject.Name))
            {
                // Display an error since there is an ongoing Editing operation and return
                InfoDialog.ShowDialog(InfoDialogProperties.GetErrorDialogProperties(Resources.TaskPaneEditingNotPossibleTitleText, string.Format(Resources.TableWithOperationOngoingError, schemaAndTableNames)));
                return(false);
            }

            // Preview the table's data in case the user option for that is on
            if (!fromSavedConnectionInfo && Settings.Default.EditPreviewMySqlData)
            {
                using (var previewDataDialog = new PreviewTableViewDialog(tableObject, true))
                {
                    if (previewDataDialog.ShowDialog() == DialogResult.Cancel)
                    {
                        return(false);
                    }
                }
            }

            // Check if selected Table has a Primary Key, it it does not we prompt an error and exit since Editing on such table is not permitted
            if (!WbConnection.TableHasPrimaryKey(tableObject.Name))
            {
                InfoDialog.ShowDialog(InfoDialogProperties.GetErrorDialogProperties(Resources.EditOpenSatusError, Resources.EditOpenSummaryError, Resources.EditOpenDetailsError));
                return(false);
            }

            // Attempt to Import Data unless the user cancels the import operation
            var proposedWorksheetName = fromSavedConnectionInfo ? tableObject.Name : Globals.ThisAddIn.ActiveWorkbook.GetWorksheetNameAvoidingDuplicates(tableObject.Name);

            tableObject.ImportParameters.ForEditDataOperation = true;
            MySqlDataTable mySqlTable;

            using (var importForm = new ImportTableViewForm(tableObject, proposedWorksheetName))
            {
                if (importForm.ImportHidingDialog() == DialogResult.Cancel)
                {
                    return(false);
                }

                mySqlTable = importForm.MySqlTable;
            }

            if (mySqlTable == null || mySqlTable.Columns.Count == 0)
            {
                MiscUtilities.ShowCustomizedErrorDialog(string.Format(Resources.UnableToRetrieveData, tableObject.Name));
                return(false);
            }

            var activeWorkbookEditConnectionInfos = WorkbookConnectionInfos.GetWorkbookEditConnectionInfos(Globals.ThisAddIn.ActiveWorkbook);

            // Hide all other open EditDataDialog forms before opening a new one.
            if (!fromSavedConnectionInfo)
            {
                foreach (var connectionInfo in activeWorkbookEditConnectionInfos.Where(connectionInfo => connectionInfo.EditDialog != null && connectionInfo.EditDialog.Visible))
                {
                    connectionInfo.EditDialog.Hide();
                }
            }

            // Create the new Excel Worksheet and import the editing data there
            var editWorkbook     = fromSavedConnectionInfo && workbook != null ? workbook : Globals.ThisAddIn.ActiveWorkbook;
            var currentWorksheet = fromSavedConnectionInfo && Settings.Default.EditSessionsReuseWorksheets
        ? editWorkbook.GetOrCreateWorksheet(proposedWorksheetName, true)
        : editWorkbook.CreateWorksheet(proposedWorksheetName, true);

            if (currentWorksheet == null)
            {
                return(false);
            }

            // Clear the contents of the worksheet if we are restoring a saved <see cref="EditConnectionInfo"/> since the user may have input data into it.
            if (fromSavedConnectionInfo)
            {
                currentWorksheet.UsedRange.Clear();
            }

            // Create and show the Edit Data Dialog
            var editConnectionInfo = GetEditConnectionInfo(mySqlTable, currentWorksheet);

            ActiveEditDialog = editConnectionInfo.EditDialog;
            if (fromSavedConnectionInfo)
            {
                // If restoring EditConnectionInfo objects we need to create and link their corresponding EditDialog to it.
                var editConnectionInfoBeingRestored = activeWorkbookEditConnectionInfos.FirstOrDefault(connectionInfo => connectionInfo.TableName.Equals(editConnectionInfo.TableName));
                if (editConnectionInfoBeingRestored != null)
                {
                    editConnectionInfoBeingRestored.EditDialog = editConnectionInfo.EditDialog;
                }
            }
            else
            {
                ActiveEditDialog.ShowDialog();

                // If not restoring EditConnectionInfo objects we need to add the manually triggered EditConnectionInfo to the list of the active workbook.
                activeWorkbookEditConnectionInfos.Add(editConnectionInfo);
            }

            return(true);
        }
Пример #2
0
        /// <summary>
        /// Event delegate method fired when <see cref="ImportDataHotLabel"/> is clicked.
        /// </summary>
        /// <param name="sender">Sender object.</param>
        /// <param name="e">Event arguments.</param>
        private void ImportDataHotLabel_Click(object sender, EventArgs e)
        {
            var selectedNode   = DBObjectList.SelectedNode;
            var parentTaskPane = Parent as ExcelAddInPane;

            if (selectedNode == null || parentTaskPane == null || selectedNode.Type != MySqlListViewNode.MySqlNodeType.DbObject || selectedNode.DbObject == null || _wbConnection == null)
            {
                return;
            }

            var passwordFlags = _wbConnection.TestConnectionAndRetryOnWrongPassword();

            if (!passwordFlags.ConnectionSuccess)
            {
                return;
            }

            if (parentTaskPane.ActiveWorksheetInEditMode)
            {
                var dr = MiscUtilities.ShowCustomizedWarningDialog(Resources.WorksheetInEditModeWarningTitle, Resources.WorksheetInEditModeWarningDetail);
                if (dr != DialogResult.Yes)
                {
                    return;
                }

                var newWorksheet = Globals.ThisAddIn.ActiveWorkbook.CreateWorksheet(selectedNode.DbObject.Name, true);
                if (newWorksheet == null)
                {
                    return;
                }
            }

            try
            {
                DialogResult dr = DialogResult.Cancel;
                Cursor = Cursors.WaitCursor;
                var activeWorkbook = Globals.ThisAddIn.ActiveWorkbook;
                if (selectedNode.DbObject is DbTable)
                {
                    var dbTable = selectedNode.DbObject as DbTable;
                    dbTable.ImportParameters.ForEditDataOperation = false;
                    using (var importForm = new ImportTableViewForm(dbTable, activeWorkbook.ActiveSheet.Name))
                    {
                        dr = importForm.ShowDialog();
                    }
                }
                else if (selectedNode.DbObject is DbView)
                {
                    var dbView = selectedNode.DbObject as DbView;
                    dbView.ImportParameters.ForEditDataOperation = false;
                    using (var importForm = new ImportTableViewForm(dbView, activeWorkbook.ActiveSheet.Name))
                    {
                        dr = importForm.ShowDialog();
                    }
                }
                else if (selectedNode.DbObject is DbProcedure)
                {
                    using (var importProcedureForm = new ImportProcedureForm(selectedNode.DbObject as DbProcedure, parentTaskPane.ActiveWorksheet.Name))
                    {
                        dr = importProcedureForm.ShowDialog();
                    }
                }

                if (dr == DialogResult.OK)
                {
                    RefreshActionLabelsEnabledStatus(null, false);
                }
            }
            catch (Exception ex)
            {
                MiscUtilities.ShowCustomizedErrorDialog(Resources.ImportDataErrorTitle, ex.Message, true);
                MySqlSourceTrace.WriteAppErrorToLog(ex);
            }
            finally
            {
                Cursor = Cursors.Default;
            }
        }