Наследование: System.Data.Common.DbConnectionStringBuilder
        public void ExecuteCompactDb(ICommandContext context)
        {
            ProgressWorker.Run(
                context,
                false,
                progressVisualizer =>
            {
                progressVisualizer.SetProgressText(Resources.CompactDbProgressText);

                var janusDatabaseManager = context.GetRequiredService <IJanusDatabaseManager>();

                using (janusDatabaseManager.GetLock().GetWriterLock())
                {
                    var connectionString = janusDatabaseManager.GetCurrentConnectionString();
                    var csb        = new JetConnectionStringBuilder(connectionString);
                    var dbFile     = csb.DataSource;
                    var tmpFile    = dbFile + ".temp";
                    var backupFile = dbFile + ".bak";

                    csb.DataSource = tmpFile;

                    if (File.Exists(tmpFile))
                    {
                        File.Delete(tmpFile);
                    }

                    OleDbConnection.ReleaseObjectPool();

                    var engine = (IJetEngine) new JetEngineClass();
                    try
                    {
                        engine.CompactDatabase(connectionString, csb.ConnectionString);
                    }
                    finally
                    {
                        Marshal.ReleaseComObject(engine);
                    }

                    if (Environment.OSVersion.Platform == PlatformID.Win32Windows)
                    {
                        // Unsafe replace
                        //
                        File.Move(tmpFile, dbFile);
                        File.Delete(tmpFile);
                    }
                    else
                    {
                        // Safe replace
                        //
                        File.Replace(tmpFile, dbFile, backupFile);
                        File.Delete(backupFile);
                    }
                }
            });
        }
Пример #2
0
		public void ExecuteCompactDb(ICommandContext context)
		{
			ProgressWorker.Run(
				context,
				false,
				progressVisualizer =>
				{
					progressVisualizer.SetProgressText(Resources.CompactDbProgressText);
					
					var janusDatabaseManager = context.GetRequiredService<IJanusDatabaseManager>();
					
					using (janusDatabaseManager.GetLock().GetWriterLock())
					{
						var connectionString = janusDatabaseManager.GetCurrentConnectionString();
						var csb = new JetConnectionStringBuilder(connectionString);
						var dbFile = csb.DataSource;
						var tmpFile = dbFile + ".temp";
						var backupFile = dbFile + ".bak";

						csb.DataSource = tmpFile;

						if (File.Exists(tmpFile))
							File.Delete(tmpFile);

						OleDbConnection.ReleaseObjectPool();

						// ReSharper disable once SuspiciousTypeConversion.Global
						var engine = (IJetEngine) new JetEngineClass();
						try
						{
							engine.CompactDatabase(connectionString, csb.ConnectionString);
						}
						finally
						{
							Marshal.ReleaseComObject(engine);
						}

						if (Environment.OSVersion.Platform == PlatformID.Win32Windows)
						{
							// Unsafe replace
							//
							File.Move(tmpFile, dbFile);
							File.Delete(tmpFile);
						}
						else
						{
							// Safe replace
							//
							File.Replace(tmpFile, dbFile, backupFile);
							File.Delete(backupFile);
						}
					}
				});
		}
Пример #3
0
 public bool CheckConnectionString(string constr)
 {
     try
     {
         var csbCheck = new JetConnectionStringBuilder(constr);
         using (var con = new OleDbConnection(csbCheck.ConnectionString))
             con.Open();
     }
     catch
     {
         return(false);
     }
     return(true);
 }
Пример #4
0
		public bool CheckConnectionString(string constr)
		{
			try
			{
				var csbCheck = new JetConnectionStringBuilder(constr);
				using (var con = new OleDbConnection(csbCheck.ConnectionString))
					con.Open();
			}
			catch
			{
				return false;
			}
			return true;
		}
Пример #5
0
        public static DBSchema LoadSchema(string connStr)
        {
            var dbsc = new DBSchema();

            var catalog = (Catalog) new CatalogClass();
            var conn    = new ConnectionClass();

            try
            {
                var csb = new JetConnectionStringBuilder(connStr);

                var keysList    = new List <KeySchema>();
                var indexesList = new List <IndexSchema>();

                conn.Open(csb.ConnectionString, string.Empty, string.Empty, 0);
                catalog.ActiveConnection = conn;

                var slashPos = csb.DataSource.LastIndexOf(@"\");

                dbsc.Name = csb.DataSource.Substring(slashPos == -1 ? 0 : slashPos + 1);

                foreach (var xTable in GetTables(catalog))
                {
                    keysList.Clear();
                    indexesList.Clear();

                    var eTable = new TableSchema
                    {
                        Name    = xTable.Name,
                        Columns = GetColumns(xTable).ToArray(),
                        Keys    = GetKeys(xTable).ToArray(),
                        Indexes = GetIndexes(xTable).ToArray()
                    };

                    // Фильтрация ключей
                    foreach (var key in eTable.Keys)
                    {
                        // Долбаный Аксес создает при создании внешних ключей уникальные индексы.
                        if (key.KeyType == ConstraintType.KeyForeign && !eTable.IsIndexExist(key.Name))
                        {
                            continue;
                        }
                        if (key.KeyType == ConstraintType.Unique &&
                            eTable.IsKeyExist(key.Name, ConstraintType.KeyForeign))
                        {
                            continue;
                        }

                        keysList.Add(key);
                    }
                    eTable.Keys = keysList.ToArray();

                    // Фильтрация индексов
                    foreach (var index in eTable.Indexes)
                    {
                        if (eTable.IsKeyExist(index.Name, ConstraintType.Unique) ||
                            eTable.IsKeyExist(index.Name, ConstraintType.KeyPrimary) ||
                            eTable.IsKeyExist(index.Name, ConstraintType.KeyForeign))
                        {
                            continue;
                        }

                        indexesList.Add(index);
                    }
                    eTable.Indexes = indexesList.ToArray();

                    dbsc.Tables.Add(eTable);
                }
            }
            finally
            {
                Marshal.ReleaseComObject(conn);
                Marshal.ReleaseComObject(catalog);
            }

            return(dbsc);
        }
Пример #6
0
		public static DBSchema LoadSchema(string connStr)
		{
			var dbsc = new DBSchema();

			var catalog = (Catalog)new CatalogClass();
			var conn = new ConnectionClass();
			try
			{
				var csb = new JetConnectionStringBuilder(connStr);

				var keysList = new List<KeySchema>();
				var indexesList = new List<IndexSchema>();

				conn.Open(csb.ConnectionString, string.Empty, string.Empty, 0);
				catalog.ActiveConnection = conn;

				var slashPos = csb.DataSource.LastIndexOf(@"\");

				dbsc.Name = csb.DataSource.Substring(slashPos == -1 ? 0 : slashPos + 1);

				foreach (var xTable in GetTables(catalog))
				{
					keysList.Clear();
					indexesList.Clear();

					var eTable = new TableSchema
					{
						Name = xTable.Name,
						Columns = GetColumns(xTable).ToArray(),
						Keys = GetKeys(xTable).ToArray(),
						Indexes = GetIndexes(xTable).ToArray()
					};

					// Фильтрация ключей
					foreach (var key in eTable.Keys)
					{
						// Долбаный Аксес создает при создании внешних ключей уникальные индексы.
						if (key.KeyType == ConstraintType.KeyForeign && !eTable.IsIndexExist(key.Name))
							continue;
						if (key.KeyType == ConstraintType.Unique &&
							eTable.IsKeyExist(key.Name, ConstraintType.KeyForeign))
							continue;

						keysList.Add(key);
					}
					eTable.Keys = keysList.ToArray();

					// Фильтрация индексов
					foreach (var index in eTable.Indexes)
					{
						if (eTable.IsKeyExist(index.Name, ConstraintType.Unique) ||
							eTable.IsKeyExist(index.Name, ConstraintType.KeyPrimary) ||
								eTable.IsKeyExist(index.Name, ConstraintType.KeyForeign))
							continue;

						indexesList.Add(index);
					}
					eTable.Indexes = indexesList.ToArray();

					dbsc.Tables.Add(eTable);
				}
			}
			finally
			{
				Marshal.ReleaseComObject(conn);
				Marshal.ReleaseComObject(catalog);
			}

			return dbsc;
		}