private void AddFiles() { var dlg = new OpenFileDialog(); dlg.Multiselect = true; dlg.Filter = "eGaz 2.0 files (*.mdb)|*.mdb|All files (*.*)|*.*"; if (dlg.ShowDialog().ValueOrFalse()) { foreach (string filename in dlg.FileNames) { var job = new GazetteerConversionTarget { SourceFile = filename, TargetFile = CalculateTargetName(filename) }; _jobList.Add(job); } } }
private void ConvertFile(GazetteerConversionTarget job) { // First try and connect to the source file... try { job.SettingsCount = 0; job.DivisionCount = 0; job.PlaceCount = 0; job.Errors = 0; using (var con = new OleDbConnection(@"Provider=Microsoft.JET.OLEDB.4.0;data source=" + job.SourceFile + ";Jet OLEDB:Database Password=123456")) { con.Open(); OleDbCommand cmd; StatusMessage("Checking source file..."); using (cmd = new OleDbCommand("SELECT SettingValue from tblSettings WHERE SettingKey = 'DatabaseVersion'", con)) { using (OleDbDataReader reader = cmd.ExecuteReader()) { if (reader.Read()) { var version = reader[0] as string; if (!"2.0".Equals(version, StringComparison.CurrentCultureIgnoreCase)) { ErrorMessage.Show("Unexpected version number found in source file: " + version + ". Converstion aborted."); return; } } else { ErrorMessage.Show("Invalid gazetteer file. Could not select from settings table. Conversion aborted."); } } // If we get here we have a valid gaz file, so create the new file... var service = new GazetteerService(job.TargetFile, true); // begin a transaction for all the inserts... service.BeginTransaction(); StatusMessage("Converting Settings..."); using (cmd = new OleDbCommand("SELECT SettingKey, SettingValue, LongData, UseLongData from tblSettings", con)) { using (OleDbDataReader reader = cmd.ExecuteReader()) { while (reader.Read()) { var key = reader[0] as string; var useLongData = (bool)reader[3]; string value = null; if (!useLongData) { value = reader[1] as string; } else { value = reader[2] as string; } service.SetSetting(key, value, useLongData); job.SettingsCount++; } } } service.SetSetting("DatabaseVersion", "3.0"); // Now copy over divisions... StatusMessage("Converting Divisions..."); using (cmd = new OleDbCommand("SELECT tDatabase, tAbbreviation, tFull from tblDivisions", con)) { using (OleDbDataReader reader = cmd.ExecuteReader()) { while (reader.Read()) { service.AddDivision(reader[0] as string, reader[1] as string, reader[2] as string); job.DivisionCount++; } } } // Now count the number of place names... StatusMessage("Counting Place Names..."); int totalPlaceNames = 0; using (cmd = new OleDbCommand("SELECT count(*) from tblGaz", con)) { using (OleDbDataReader reader = cmd.ExecuteReader()) { if (reader.Read()) { totalPlaceNames = (int)reader[0]; } } } progressBar.InvokeIfRequired(() => { progressBar.Value = 0; progressBar.Maximum = totalPlaceNames; }); if (totalPlaceNames > 0) { StatusMessage("Converting Place Names..."); using (cmd = new OleDbCommand("SELECT tPlace, tType, tDivision, tLatitude, tLongitude, dblLatitude, dblLongitude from tblGaz", con)) { using (OleDbDataReader reader = cmd.ExecuteReader()) { while (reader.Read()) { double lat = 0; if (!reader.IsDBNull(5)) { lat = (double)reader[5]; } double lon = 0; if (!reader.IsDBNull(6)) { lon = (double)reader[6]; } try { service.AddPlaceName(reader[0] as string, reader[1] as string, reader[2] as string, reader[3] as string, reader[4] as string, lat, lon); } catch (Exception) { job.Errors++; } job.PlaceCount++; progressBar.InvokeIfRequired(() => { progressBar.Value = job.PlaceCount; }); } } } } service.CommitTransaction(); StatusMessage("Complete."); progressBar.InvokeIfRequired(() => { progressBar.Value = 0; }); } } // MessageBox.Show( string.Format("Conversion complete with {3} errors: \n{0} Settings\n{1} Divisions\n{2} Place names.", job.SettingsCount, job.DivisionCount, job.PlaceCount, job.Errors), "Conversion complete", MessageBoxButton.OK, MessageBoxImage.Information); } catch (Exception ex) { ErrorMessage.Show(ex.ToString()); } }