public void bazaCompact() { string sciezka = System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location); string oldFileName = sciezka + "\\SIS_DB.accdb"; string newFileName = sciezka + "\\_SIS_DB.accdb"; DBEngine db = new DBEngine(); db.CompactDatabase(sciezka + "\\SIS_DB.accdb", sciezka + "\\_SIS_DB.accdb"); File.Delete(oldFileName); // Move (rename) the temporary compacted database to // the original filename File.Move(newFileName, oldFileName); // The operation was successful }
//Сжатие базы данных public static void Compress(string file, //файл базы int size, //размер а байтах, после которого нужно сжимать string tmpDir = null, //каталог временных фалов int timeout = 0) //время ожидания после сжатия в мс { if (file.IsEmpty()) { throw new NullReferenceException("Файл сжимаемой базы данных не может быть пустой строкой или null"); } var fdb = new FileInfo(file); if (fdb.Length < size) { return; } string sdir = fdb.Directory.FullName; if (tmpDir != null) { var dir = new DirectoryInfo(tmpDir); if (!dir.Exists) { dir.Create(); } sdir = tmpDir; } var ftmp = new FileInfo(sdir + @"\Tmp" + fdb.Name); if (ftmp.Exists) { ftmp.Delete(); } fdb.MoveTo(ftmp.FullName); new FileInfo(file).Delete(); var en = new DBEngine(); en.CompactDatabase(ftmp.FullName, file); en.FreeLocks(); en = null; GC.Collect(); if (timeout > 0) { Thread.Sleep(timeout); } }
private void 压缩数据库ToolStripMenuItem_Click(object sender, EventArgs e) { string accessFile = "iReminder.mdb"; string tempFile = Path.Combine(Path.GetDirectoryName(accessFile), Path.GetRandomFileName() + Path.GetExtension(accessFile)); var dbe = new DBEngine(); try { dbe.CompactDatabase(accessFile, tempFile); FileInfo temp = new FileInfo(tempFile); temp.CopyTo(accessFile, true); temp.Delete(); } catch (Exception e1) { Console.WriteLine("Error: " + e1.Message); } MessageBox.Show("完成压缩!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Asterisk); }