/// <summary> /// Handles event when Manage DSN button is clicked /// </summary> /// <param name="param"></param> /// <remarks></remarks> private void ManageDsnCommandClick(object param) { //DispatcherHelper.DoEvents(); OdbcCP32 odbccp32 = new OdbcCP32(); bool result = odbccp32.ManageDatasources(_windowHandle); OnPropertyChanged("DsnList"); }
public static bool CreateScratchMdb(HluDataSet.incidDataTable incidTable, HluDataSet.incid_mm_polygonsDataTable incidMMTable) { try { _incidTable = incidTable; _incidMMTable = incidMMTable; _scratchMdbPath = String.Empty; try { _scratchMdbPath = Path.GetTempPath(); } catch { _scratchMdbPath = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData); _scratchMdbPath += Path.DirectorySeparatorChar.ToString(); } _scratchMdbPath += Path.GetFileNameWithoutExtension(Path.GetRandomFileName()) + ".mdb"; OdbcCP32 odbc = new OdbcCP32(); odbc.CreateDatabase(_scratchMdbPath); string connString = String.Format(@"Provider=Microsoft.Jet.OLEDB.4.0;Data Source={0};", _scratchMdbPath); string defaultSchema = ""; bool promptPwd = false; _scratchDb = new DbOleDb(ref connString, ref defaultSchema, ref promptPwd, Properties.Resources.PasswordMaskString, Settings.Default.UseAutomaticCommandBuilders, true, Settings.Default.DbIsUnicode, Settings.Default.DbUseTimeZone, 255, Settings.Default.DbBinaryLength, Settings.Default.DbTimePrecision, Settings.Default.DbNumericPrecision, Settings.Default.DbNumericScale); return(true); } catch { if (File.Exists(_scratchMdbPath)) { try { if ((_scratchDb != null) && (_scratchDb.Connection.State != ConnectionState.Closed)) { _scratchDb.Connection.Close(); } File.Delete(_scratchMdbPath); } catch { } } return(false); } }
private string ExportMdb(string sql, DataTable exportTable, int incidOrdinal, List <int> fieldCountList, out int exportRowCount) { exportRowCount = -1; DbOleDb dbOut = null; string tempPath = String.Empty; try { tempPath = Path.GetTempPath(); } catch { tempPath = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData); } tempPath = Path.Combine(tempPath, Path.GetFileNameWithoutExtension(Path.GetRandomFileName()) + ".mdb"); try { if (File.Exists(tempPath)) { File.Delete(tempPath); } OdbcCP32 odbc = new OdbcCP32(); odbc.CreateDatabase(tempPath); string connString = String.Format(@"Provider=Microsoft.Jet.OLEDB.4.0;Data Source={0};", tempPath); string defaultSchema = ""; bool promptPwd = false; dbOut = new DbOleDb(ref connString, ref defaultSchema, ref promptPwd, Properties.Resources.PasswordMaskString, Settings.Default.UseAutomaticCommandBuilders, true, Settings.Default.DbIsUnicode, Settings.Default.DbUseTimeZone, 255, Settings.Default.DbBinaryLength, Settings.Default.DbTimePrecision, Settings.Default.DbNumericPrecision, Settings.Default.DbNumericScale); dbOut.CreateTable(exportTable); DataSet datasetOut = new DataSet("Export"); IDbDataAdapter adapterOut = dbOut.CreateAdapter(exportTable); adapterOut.Fill(datasetOut); int[] pkOrdinals = exportTable.PrimaryKey.Select(c => c.Ordinal).ToArray(); exportTable.PrimaryKey = pkOrdinals.Select(o => exportTable.Columns[o]).ToArray(); adapterOut.TableMappings.Clear(); adapterOut.TableMappings.Add(exportTable.TableName, datasetOut.Tables[0].TableName); exportTable = datasetOut.Tables[0]; DataRow exportRow = null; bool rowAdded = false; using (IDataReader reader = _viewModelMain.DataBase.ExecuteReader(sql, _viewModelMain.DataBase.Connection.ConnectionTimeout, CommandType.Text)) { int runTotal = 0; int[][] fieldMapTemplate = fieldCountList.Select((i, index) => new int[] { index, index + runTotal, i, (runTotal += i - (i > 0 ? 1 : 0)) }) .Select(e => new int[] { e[0], e[1], e[1] + e[2] }).ToArray(); int[] dupsAllowed = (from e in exportTable.Columns.Cast <DataColumn>() let q = from c in _viewModelMain.HluDataset.incid_sources.Columns.Cast <DataColumn>() where !Regex.IsMatch(c.ColumnName, @"(\Aincid\z|_(importance|id)\z)", RegexOptions.IgnoreCase) select c.ColumnName where q.Count(n => Regex.IsMatch(e.ColumnName, n + @"(_[0-9]+)*\z", RegexOptions.IgnoreCase)) == 1 select e.Ordinal).ToArray(); int[][] fieldMap = new int[fieldMapTemplate.Length][]; string currIncid = String.Empty; string prevIncid = String.Empty; while (reader.Read()) { currIncid = reader.GetString(incidOrdinal); if (currIncid != prevIncid) { prevIncid = currIncid; fieldMap = fieldMapTemplate.Select(a => new int[] { a[0], a[1], a[2] }).ToArray(); if (exportRow != null) { exportTable.Rows.Add(exportRow); rowAdded = true; } exportRow = exportTable.NewRow(); rowAdded = false; for (int i = 0; i < fieldMap.GetLength(0); i++) { object item = reader.GetValue(fieldMap[i][0]); if (item != DBNull.Value) { exportRow[fieldMap[i][1]] = reader.GetValue(fieldMap[i][0]); } fieldMap[i][1]++; } } else { for (int i = 0; i < fieldMap.GetLength(0); i++) { if (fieldMap[i][1] < fieldMap[i][2]) { object item = reader.GetValue(fieldMap[i][0]); if ((item != DBNull.Value) && (!item.Equals(exportRow[fieldMap[i][1] - 1]) || (Array.IndexOf(dupsAllowed, fieldMap[i][1]) != -1))) { exportRow[fieldMap[i][1]++] = item; } } } } } } if (!rowAdded && (exportRow != null)) { exportTable.Rows.Add(exportRow); } exportRowCount = adapterOut.Update(datasetOut); return(exportRowCount != -1 ? tempPath : null); } catch { if (File.Exists(tempPath)) { try { File.Delete(tempPath); } catch { _viewModelMain.ExportMdbs.Add(tempPath); } } return(null); } finally { if ((dbOut != null) && (dbOut.Connection.State != ConnectionState.Closed)) { try { dbOut.Connection.Close(); } catch { } } } }