/// <summary> /// 压缩Access /// </summary> private void ajaxCompactAccess() { doh.Dispose(); string tempPath = Server.MapPath(Application["jecmsV161_dbPath"].ToString()); string _dbPath = JumboECMS.Utils.DirFile.GetFolderPath(tempPath); string _dbName = JumboECMS.Utils.DirFile.GetFileName(tempPath); if (!System.IO.File.Exists(tempPath)) { this._response = JsonResult(0, "目标数据库不存在"); return; } try { string temp = _dbPath + DateTime.Now.Year.ToString("yyyyMMddHHmmssffff") + ".bak"; string temp2 = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + temp; string _dbPath2 = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + tempPath; JRO.JetEngineClass jt = new JRO.JetEngineClass(); jt.CompactDatabase(_dbPath2, temp2); System.IO.File.Copy(temp, tempPath, true); System.IO.File.Delete(temp); } catch (Exception) { this._response = JsonResult(0, "其它用户连接数据库"); return; } this._response = JsonResult(1, "数据库压缩成功"); }
///压缩修复ACCESS数据库,mdbPath为数据库绝对路径 public static void Compact(object mdbPath_data) { try { string mdbPath = (string)mdbPath_data; System.IO.FileInfo f = new FileInfo(mdbPath); //if (f.Length > 50000000) //{ //Debug.WriteLine("数据库压缩前的文件大小:" + f.Length.ToString()); //if (con != null) //{ // con.Close(); // con.Dispose(); //} if (mdbPath == "") { // mdbPath = GetAccessFileName(); } if (!File.Exists(mdbPath)) //检查数据库是否已存在 { throw new Exception("目标数据库不存在,无法压缩"); } // Thread.Sleep(1000); //声明临时数据库的名称 string temp = DateTime.Now.Year.ToString(); temp += DateTime.Now.Month.ToString(); temp += DateTime.Now.Day.ToString(); temp += DateTime.Now.Hour.ToString(); temp += DateTime.Now.Minute.ToString(); temp += DateTime.Now.Second.ToString() + ".bak"; temp = mdbPath.Substring(0, mdbPath.LastIndexOf("\\") + 1) + temp; //定义临时数据库的连接字符串 string temp2 = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + temp; //定义目标数据库的连接字符串 string mdbPath2 = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + mdbPath + ";"; //创建一个JetEngineClass对象的实例 JRO.JetEngineClass jt = new JRO.JetEngineClass(); //使用JetEngineClass对象的CompactDatabase方法压缩修复数据库 jt.CompactDatabase(mdbPath2, temp2); //拷贝临时数据库到目标数据库(覆盖) File.Copy(temp, mdbPath, true); //最后删除临时数据库 File.Delete(temp); //} } catch (Exception er) { // MessageBox.Show(er.Message); } }