private void ImportSelectionFromFile(BulkDataOperationBase importer, Table dbTable) { CopyCSVToMemSchema(dbTable); foreach (Table memTable in MemSchema.Tables) { importer.WorkTables.Add(memTable); memTable.Process = true; } UpdateProgressBar(ImportProgressBar, 0); importer.OverallProgress += Importer_OverallProgress; importer.UseFromSchemaReferentialIntegrity = false; importer.Execute(); UpdateProgressBar(ImportProgressBar, 100); }
private void ImportButton_Click(object sender, EventArgs e) { Table table = (Table)ImportTableComboBox.SelectedItem; if ((object)table != null) { ImportFileDialog.FileName = table.Name; } DialogResult result = ImportFileDialog.ShowDialog(); if (result != DialogResult.OK) { return; } string fileName = Path.GetFileName(ImportFileDialog.FileName); string fileNameWithoutExtension = Path.GetFileNameWithoutExtension(fileName); Table matchingTable = DBSchema.Tables[fileNameWithoutExtension]; // Each of the import operations require slightly different wording Dictionary <object, string> importPhraseLookup = new Dictionary <object, string>() { { InsertButton, "insert data into" }, { UpdateButton, "update" }, { DeleteButton, "delete data from" } }; if (!importPhraseLookup.TryGetValue(sender, out string importPhrase)) { importPhrase = "import data into"; } // Run sanity checks to make sure the user didn't make a mistake if ((object)table != null && (object)matchingTable != null && !ReferenceEquals(table, matchingTable)) { DialogResult matchResult = MessageBox.Show($"File name matches another table in the database. Would you like to {importPhrase} the {matchingTable.Name} table instead?", "Matching table detected", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question); if (matchResult == DialogResult.Cancel) { return; } if (matchResult == DialogResult.Yes) { ImportTableComboBox.SelectedItem = table = matchingTable; } } else if ((object)table != null) { DialogResult confirmResult = MessageBox.Show($"You are about to {importPhrase} the {table.Name} table. Are you sure you would like to proceed?", "Confirm import", MessageBoxButtons.YesNo, MessageBoxIcon.Question); if (confirmResult == DialogResult.No) { return; } } else { MessageBox.Show("Please select a table to import to."); return; } // Disabling the tab control makes it so the user cannot interact with any // controls while the import is executing, but running in a separate thread // does allow the user to move the window around while the import executes Cursor cursor = Cursor; BeginImport(); Thread t = new Thread(() => { try { DoImport(); } finally { EndImport(); } }); t.IsBackground = true; t.Start(); void BeginImport() { if (InvokeRequired) { Invoke(new Action(BeginImport)); return; } MainTabControl.Enabled = false; Cursor = Cursors.WaitCursor; ImportProgressBar.Value = 0; } void DoImport() { using (BulkDataOperationBase importer = GetImporter(sender)) { OpenBothConnectionsAndExecute(() => ImportSelectionFromFile(importer, table)); } } void EndImport() { if (InvokeRequired) { Invoke(new Action(EndImport)); return; } MainTabControl.Enabled = true; Cursor = cursor; ImportProgressBar.Value = 100; MessageBox.Show($"Completed import from {fileName} to the {table.Name} table."); } }