Пример #1
0
 private bool CompactDB(string srcDB, string dstDB, string pass)
 {
     try
     {
         string sourceConnection = string.Concat(new string[]
         {
             "Provider=Microsoft.Jet.OLEDB.4.0; Data Source=",
             srcDB,
             "; Jet OLEDB:Database Password="******";"
         });
         string destconnection = string.Concat(new string[]
         {
             "Provider=Microsoft.Jet.OLEDB.4.0; Data Source=",
             dstDB,
             "; Jet OLEDB:Database Password="******";"
         });
         JetEngine jetEngine = (JetEngine)Activator.CreateInstance(Type.GetTypeFromCLSID(new Guid("DE88C160-FF2C-11D1-BB6F-00C04FAE22DA")));
         jetEngine.CompactDatabase(sourceConnection, destconnection);
         return(true);
     }
     catch (Exception)
     {
     }
     return(false);
 }
Пример #2
0
        public static void CompactJetDatabase(string fileName)
        {
            // I use this function as part of an AJAX page, so rather
            // than throwing exceptions if errors are encountered, I
            // simply return false and allow the page to handle the
            // failure generically.
            try
            {
                // Find the database on the web server
                string oldFileName = fileName;

                // JET will not compact the database in place, so we
                // need to create a temporary filename to use
                string newFileName =
                    Path.Combine(Path.GetDirectoryName(oldFileName),
                                 Guid.NewGuid().ToString("N") + ".mdb");

                // Obtain a reference to the JET engine


                JetEngine engine =
                    (JetEngine)HttpContext.Current.Server.CreateObject(
                        "JRO.JetEngine");

                // Compact the database (saves the compacted version to
                // newFileName)

                engine.CompactDatabase(
                    String.Format(
                        AccessOleDbConnectionStringFormat, oldFileName),
                    String.Format(
                        AccessOleDbConnectionStringFormat, newFileName));

                // Delete the original database
                File.Delete(oldFileName);

                // Move (rename) the temporary compacted database to
                // the original filename
                File.Move(newFileName, oldFileName);

                // The operation was successful
            }
            catch (Exception ex)
            {
                // We encountered an error
                MessageBox.Show("Greska : " + ex.Message);
            }
        }
Пример #3
0
        /// <summary>
        /// Оптимизирует базу данных текущего года
        /// посредством удаления устаревших данных
        /// и последующего сжатия файла базы данных.
        /// </summary>
        public static void Optimize()
        {
            Load(Connection);
            Tables.Optimize();
            Clear();

            // Сжатие базы данных
            string tempPath = Path.Combine(_DataDirectory.FullName, "temp.mdb");

            _ConnectionStringBuilder.DataSource = tempPath;
            JRO.JetEngine jro = new JetEngine();
            jro.CompactDatabase(Connection.ConnectionString, _ConnectionStringBuilder.ConnectionString);
            File.Delete(Connection.DataSource);
            File.Copy(tempPath, Connection.DataSource);
            File.Delete(tempPath);
        }
Пример #4
0
 //压缩数据库
 public void compressDB_B(ToolStripStatusLabel label, string db_Pwd)
 {
     try
     {
         JetEngine x        = new JetEngine();
         string    connstr  = string.Format(@"Provider=Microsoft.Jet.OLEDB.4.0;Data Source={0}\source\StuContact.mdb;Jet OLEDB:Database Password='******'", Application.StartupPath, db_Pwd);
         string    connstr1 = string.Format(@"Provider=Microsoft.Jet.OLEDB.4.0;Data Source={0}\source\StuContact1.mdb;Jet OLEDB:Database Password='******'", Application.StartupPath, db_Pwd);
         x.CompactDatabase(connstr, connstr1);
         File.Delete(Application.StartupPath + "\\source\\StuContact.mdb");
         File.Move(Application.StartupPath + "\\source\\StuContact1.mdb", Application.StartupPath + "\\source\\StuContact.mdb");
         MessageBox.Show("数据库压缩成功!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
         refresh_DB(label);//调用方法刷新数据库大小显示
     }
     catch (Exception ex)
     {
         MessageBox.Show(ex.Message, "出错提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
     }
 }
Пример #5
0
        public static bool CompactAndRepair()
        {
            try
            {
                JetEngine engine = new JetEngine();
                engine.CompactDatabase(connectionString, tempConnectionString);
                File.Delete(DBPath);
                File.Move(DBPath, DBPath);

                Console.WriteLine("Database successfully Repaired and Compacted\n");
                return(true);
            }
            catch (Exception ex)
            {
                Console.WriteLine("Database failed to Repair and Compact\n" + ex.ToString());
            }
            return(false);
        }
Пример #6
0
    //压缩
    protected void Button2_Click(object sender, EventArgs e)
    {
        string DbPath1, DbPath2, DbConn1, DbConn2;

        DbPath1 = Server.MapPath("/App_Data/DataBase.mdb");  //原数据库路径
        DbPath2 = Server.MapPath("/App_Data/DataBase2.mdb"); //压缩后的数据库路径
        DbConn1 = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + DbPath1;
        DbConn2 = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + DbPath2;

        try
        {
            JetEngine DatabaseEngin = new JetEngine();
            DatabaseEngin.CompactDatabase(DbConn1, DbConn2); //压缩

            File.Copy(DbPath2, DbPath1, true);               //将压缩后的数据库覆盖原数据库
            File.Delete(DbPath2);                            //删除压缩后的数据库

            SiteD.Common.Jscript.Alert("数据库压缩成功!");
        }
        catch
        {
            SiteD.Common.Jscript.Alert("数据库压缩失败,请重试!");
        }
    }