public static bool CompactDB(string source) { try { JRO.JetEngine jro = new JRO.JetEngineClass(); string destTemp = source.Replace(".mdb", "-temp.mdb"); // Create connection strings string sourceConn = "PROVIDER=Microsoft.Jet.OLEDB.4.0;Data Source=" + source; string destConn = "PROVIDER=Microsoft.Jet.OLEDB.4.0;Data Source=" + destTemp + ";Jet OLEDB.Engine Type=5"; // Apply compact and repair jro.CompactDatabase(sourceConn, destConn); // Replace original source file FileInfo fi = new FileInfo(destTemp); fi.CopyTo(source, true); fi.Delete(); return(true); } catch (System.Runtime.InteropServices.COMException e) { if (e.ErrorCode == -2147467259) { throw(new MSJetUtilityLockedException("Database compaction failed; database locked. '" + e.ErrorCode.ToString() + "'", e)); } else { throw(e); } } }
public static bool CompactDB(string source, string destination) { try { JRO.JetEngine jro = new JRO.JetEngineClass(); // Create connection strings string sourceConn = "PROVIDER=Microsoft.Jet.OLEDB.4.0;Data Source=" + source; string destConn = "PROVIDER=Microsoft.Jet.OLEDB.4.0;Data Source=" + destination + ";Jet OLEDB.Engine Type=5"; // Apply compact and repair jro.CompactDatabase(sourceConn, destConn); return(true); } catch (System.Runtime.InteropServices.COMException e) { if (e.ErrorCode == -2147467259) { throw(new MSJetUtilityLockedException("Database compaction failed; database locked. '" + e.ErrorCode.ToString() + "'", e)); } else { throw e; } } }
public void yas() { if (!File.Exists(strPathMdb)) //检查数据库是否已存在 { MessageBox.Show("目标数据库不存在,请先选择Access数据库文件", "操作提示"); btnRepair.Text = "开始修复"; thacc.Abort(); return; } //声明临时数据库的名称 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 = strPathMdb.Substring(0, strPathMdb.LastIndexOf("\\") + 1) + temp; //定义临时数据库的连接字符串 string temp2 = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + temp; //定义目标数据库的连接字符串 string strPathMdb2 = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + strPathMdb; //创建一个JetEngineClass对象的实例 JRO.JetEngineClass jt = new JRO.JetEngineClass(); //使用JetEngineClass对象的CompactDatabase方法压缩修复数据库 jt.CompactDatabase(strPathMdb2, temp2); //拷贝临时数据库到目标数据库(覆盖) File.Copy(temp, strPathMdb, true); //最后删除临时数据库 File.Delete(temp); MessageBox.Show("修复完成"); }
/// <summary> /// 压缩Access数据库 /// </summary> /// <param name="DBPath">数据库绝对路径</param> public static void CompactAccess(string DBPath) { if (!File.Exists(DBPath)) { throw new Exception("目标数据库不存在,无法压缩"); } //声明临时数据库名称 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 = DBPath.Substring(0, DBPath.LastIndexOf("\\") + 1) + temp; //定义临时数据库的连接字符串 string temp2 = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + temp; //定义目标数据库的连接字符串 string DBPath2 = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + DBPath; //创建一个JetEngineClass对象的实例 JRO.JetEngineClass jt = new JRO.JetEngineClass(); //使用JetEngineClass对象的CompactDatabase方法压缩修复数据库 jt.CompactDatabase(DBPath2, temp2); //拷贝临时数据库到目标数据库(覆盖) File.Copy(temp, DBPath, true); //最后删除临时数据库 File.Delete(temp); }
private void button2_Click(object sender, EventArgs e) { if (!File.Exists(strPathMdb))//检查数据库是否已存在 { MessageBox.Show("目标数据库不存在,无法压缩", "操作提示"); return; } //声明临时数据库的名称 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 = strPathMdb.Substring(0, strPathMdb.LastIndexOf("\\") + 1) + temp; //定义临时数据库的连接字符串 string temp2 = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + temp; //定义目标数据库的连接字符串 string strPathMdb2 = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + strPathMdb; JRO.JetEngineClass jt = new JRO.JetEngineClass();//创建一个JetEngineClass对象 //使用JetEngineClass对象的CompactDatabase方法压缩修复数据库 jt.CompactDatabase(strPathMdb2, temp2); File.Copy(temp, strPathMdb, true); //拷贝临时数据库到目标数据库(覆盖) File.Delete(temp); //删除临时数据库 MessageBox.Show("修复完成"); }
/// <summary> /// 压缩Access数据库 /// </summary> /// <param name="DBPath">数据库绝对路径</param> public bool CompactAccess(string DBPath) { bool Isok = true; try { if (!File.Exists(DBPath)) { throw new Exception("目标数据库不存在,无法压缩"); } //声明临时数据库名称 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 = DBPath.Substring(0, DBPath.LastIndexOf("\\") + 1) + temp; //定义临时数据库的连接字符串 string temp2 = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + temp + @";JET OLEDB:"; //";Jet OLEDB:Database Password=123456"; //定义目标数据库的连接字符串 string DBPath2 = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + DBPath + @";JET OLEDB:"; //JET OLEDB:Engine Type=5";// ";Jet OLEDB:Database Password=123456"; //创建一个JetEngineClass对象的实例 JRO.JetEngineClass jt = new JRO.JetEngineClass(); //使用JetEngineClass对象的CompactDatabase方法压缩修复数据库 jt.CompactDatabase(DBPath2, temp2); //拷贝临时数据库到目标数据库(覆盖) File.Copy(temp, DBPath, true); //最后删除临时数据库 File.Delete(temp); } catch (Exception ex) { Isok = false; Log4Helper.Error(this.GetType(), "数据库压缩Error", ex); } return(Isok); }
///压缩修复ACCESS数据库,mdbPath为数据库绝对路径 public void Compact(string mdbPath) { try { if (File.Exists(mdbPath)) //检查数据库是否已存在 { string temp = DateTime.Now.Year.ToString(); string temp2 = null; 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; //定义临时数据库的连接字符串 temp2 = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + temp + ";Jet OLEDB:Database Password=shkj;"; //定义目标数据库的连接字符串 string mdbPath2 = null; mdbPath2 = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + mdbPath + ";Jet OLEDB:Database Password=shkj;"; //创建一个JetEngineClass对象的实例 JRO.JetEngineClass jt = new JRO.JetEngineClass(); //使用JetEngineClass对象的CompactDatabase方法压缩修复数据库 jt.CompactDatabase(mdbPath2, temp2); jt = null; //拷贝临时数据库到目标数据库(覆盖) File.Copy(temp, mdbPath, true); //最后删除临时数据库 File.Delete(temp); } } catch(Exception ex) { if (ex.Message.IndexOf("由于您和其他用户试图同时改变同一数据") != -1) { try { string strfilenameTemp = mdbPath.Replace(".mdb", ".ldb"); if (File.Exists(strfilenameTemp)) { File.Delete(strfilenameTemp); } File.Delete(mdbPath); } catch { } } else if (ex.Message.IndexOf("不可识别的数据库格式") != -1) { try { string strfilenameTemp = mdbPath.Replace(".mdb", ".ldb"); if (File.Exists(strfilenameTemp)) { File.Delete(strfilenameTemp); } File.Delete(mdbPath); } catch { } //catch (Exception ee) //{ // string s = ee.Message; //} } Thread.Sleep(100); } }
///压缩修复ACCESS数据库,mdbPath为数据库绝对路径 public void Compact(string mdbPath) { try { if (File.Exists(mdbPath)) //检查数据库是否已存在 { string temp = DateTime.Now.Year.ToString(); string temp2 = null; 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; //定义临时数据库的连接字符串 temp2 = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + temp + ";Jet OLEDB:Database Password=shkj;"; //定义目标数据库的连接字符串 string mdbPath2 = null; mdbPath2 = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + mdbPath + ";Jet OLEDB:Database Password=shkj;"; //创建一个JetEngineClass对象的实例 JRO.JetEngineClass jt = new JRO.JetEngineClass(); //使用JetEngineClass对象的CompactDatabase方法压缩修复数据库 jt.CompactDatabase(mdbPath2, temp2); jt = null; //拷贝临时数据库到目标数据库(覆盖) File.Copy(temp, mdbPath, true); //最后删除临时数据库 File.Delete(temp); } } catch (Exception ex) { if (ex.Message.IndexOf("由于您和其他用户试图同时改变同一数据") != -1) { try { string strfilenameTemp = mdbPath.Replace(".mdb", ".ldb"); if (File.Exists(strfilenameTemp)) { File.Delete(strfilenameTemp); } File.Delete(mdbPath); } catch { } } else if (ex.Message.IndexOf("不可识别的数据库格式") != -1) { try { string strfilenameTemp = mdbPath.Replace(".mdb", ".ldb"); if (File.Exists(strfilenameTemp)) { File.Delete(strfilenameTemp); } File.Delete(mdbPath); } catch { } //catch (Exception ee) //{ // string s = ee.Message; //} } Thread.Sleep(100); } }