当前位置:Gxlcms > 数据库问题 > C# 压缩和修复Access数据库

C# 压缩和修复Access数据库

时间:2021-07-01 10:21:17 帮助过:6人阅读

using System; 2 using System.Data; 3 using System.Configuration; 4 using System.Collections; 5 using System.Web; 6 using System.Web.Security; 7 using System.Web.UI; 8 using System.Web.UI.WebControls; 9 using System.Web.UI.WebControls.WebParts; 10 using System.Web.UI.HtmlControls; 11 using ADOX;//该命名空间包含创建ACCESS的类(方法)--解决方案 ==> 引用 ==> 添加引用 ==> 游览找到.dll 12 using JRO; 13 using System.IO;//该命名空间包含压缩ACCESS的类(方法) 14 15 16 /// <summary> 17 /// 当使用Access中,请浏览添加引用以下两个dll 18 /// 引用C:\Program Files\Common Files\System\ado\msadox.dll,该DLL包含ADOX命名空间 19 /// 引用C:\Program Files\Common Files\System\ado\msjro.dll,该DLL包含JRO命名空间 20 /// </summary> 21 public partial class Access : System.Web.UI.Page 22 { 23 protected void Page_Load(object sender, EventArgs e) 24 { 25 26 } 27 #region 根据指定的文件名称创建Access数据库 28 /// <summary> 29 /// 根据指定的文件名称创建数据 30 /// </summary> 31 /// <param name="DBPath">绝对路径+文件名称</param> 32 public static void CreateAccess(string DBPath) 33 { 34 if (File.Exists(DBPath))//检查数据库是否已存在 35 { 36 throw new Exception("目标数据库已存在,无法创建"); 37 } 38 DBPath = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + DBPath; 39 //创建一个CatalogClass对象实例 40 ADOX.CatalogClass cat = new ADOX.CatalogClass(); 41 //使用CatalogClass对象的Create方法创建ACCESS数据库 42 cat.Create(DBPath); 43 44 } 45 #endregion 46 47 #region 压缩Access数据库 48 /// <summary> 49 /// 压缩Access数据库 50 /// </summary> 51 /// <param name="DBPath">数据库绝对路径</param> 52 public static void CompactAccess(string DBPath) 53 { 54 if (!File.Exists(DBPath)) 55 { 56 throw new Exception("目标数据库不存在,无法压缩"); 57 } 58 59 //声明临时数据库名称 60 string temp = DateTime.Now.Year.ToString(); 61 temp += DateTime.Now.Month.ToString(); 62 temp += DateTime.Now.Day.ToString(); 63 temp += DateTime.Now.Hour.ToString(); 64 temp += DateTime.Now.Minute.ToString(); 65 temp += DateTime.Now.Second.ToString() + ".bak"; 66 temp = DBPath.Substring(0, DBPath.LastIndexOf("\\") + 1) + temp; 67 //定义临时数据库的连接字符串 68 string temp2 = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + temp; 69 //定义目标数据库的连接字符串 70 string DBPath2 = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + DBPath; 71 //创建一个JetEngineClass对象的实例 72 JRO.JetEngineClass jt = new JRO.JetEngineClass(); 73 //使用JetEngineClass对象的CompactDatabase方法压缩修复数据库 74 jt.CompactDatabase(DBPath2, temp2); 75 //拷贝临时数据库到目标数据库(覆盖) 76 File.Copy(temp, DBPath, true); 77 //最后删除临时数据库 78 File.Delete(temp); 79 } 80 #endregion 81 82 #region 备份Access数据库 83 /// <summary> 84 /// 备份Access数据库 85 /// </summary> 86 /// <param name="srcPath">要备份的数据库绝对路径</param> 87 /// <param name="aimPath">备份到的数据库绝对路径</param> 88 /// <returns></returns> 89 public static void Backup(string srcPath, string aimPath) 90 { 91 92 if (!File.Exists(srcPath)) 93 { 94 throw new Exception("源数据库不存在,无法备份"); 95 } 96 try 97 { 98 File.Copy(srcPath, aimPath, true); 99 } 100 catch (IOException ixp) 101 { 102 throw new Exception(ixp.ToString()); 103 } 104 105 } 106 107 #endregion 108 109 #region 还原Access数据库 110 /// <summary> 111 /// 还原Access数据库 112 /// </summary> 113 /// <param name="bakPath">备份的数据库绝对路径</param> 114 /// <param name="dbPath">要还原的数据库绝对路径</param> 115 public static void RecoverAccess(string bakPath, string dbPath) 116 { 117 if (!File.Exists(bakPath)) 118 { 119 throw new Exception("备份数据库不存在,无法还原"); 120 } 121 try 122 { 123 File.Copy(bakPath, dbPath, true); 124 } 125 catch (IOException ixp) 126 { 127 throw new Exception(ixp.ToString()); 128 } 129 } 130 #endregion 131 132 /// <summary> 133 /// 数据库备份 134 /// </summary> 135 /// <param name="sender"></param> 136 /// <param name="e"></param> 137 protected void btnDBBackup_Click(object sender, EventArgs e) 138 { 139 string dbPath = "E:\\RocLeft.mdb";//数据库文件路径 140 string backupPath ="G:\\RocLeft"+DateTime.Now.ToString("yyMMddHms")+".bak";//数据库备份到的路径 141 142 string time=DateTime.Now.ToString(); 143 try 144 { 145 Backup(dbPath, backupPath); 146 CompactAccess(backupPath);//压缩Access数据库 147 lblRes.Text += time + " " + dbPath + "成功备份到-->" + backupPath + "<br/>"; 148 } 149 catch (Exception ex) 150 { 151 lblRes.Text += time + " 备份失败!" + ex.Message + "<br/>"; 152 } 153 } 154 /// <summary> 155 /// 数据库恢复 156 /// </summary> 157 /// <param name="sender"></param> 158 /// <param name="e"></param> 159 protected void btnGetBack_Click(object sender, EventArgs e) 160 { 161 string dbPath = "G:\\" + this.fileUrl.FileName;//还原的数据库文件路径 162 string backupPath ="E:\\RocLeft.mdb";//还原的数据 163 164 string time=DateTime.Now.ToString(); 165 try 166 { 167 CreateAccess(backupPath);//根据指定的文件名称创建Access数据库 168 RecoverAccess(dbPath, backupPath); 169 lblRes.Text += time + " " + dbPath + "成功还原到-->" + backupPath + "<br/>"; 170 } 171 catch (Exception ex) 172 { 173 lblRes.Text += time + " 还原失败!" + ex.Message + "<br/>"; 174 } 175 } 176 }

 

------------------------------------------  补充分割线 ---------------------------------------------------------

using ADOX;//该命名空间包含创建ACCESS的类(方法)--解决方案 ==> 引用 ==> 添加引用 ==> 游览找到.dll  位置在C-program files - Common Files-System-ado-msado.dllusing using JRO; 下载地址:http://www.dllzj.com/index.html

 

关于压缩数据库  最好在此之前检查一遍数据库是否被占用,

解决方法:http://blog.csdn.net/sx341125/article/details/39086717

http://blog.csdn.net/baple/article/details/8131717

第二个链接提到的方法:(msdn介绍见:https://msdn.microsoft.com/zh-cn/library/system.runtime.interopservices.marshal.finalreleasecomobject(v=vs.110).aspx)

System.Runtime.InteropServices.Marshal.FinalReleaseComObject(catalog.ActiveConnection);  
System.Runtime.InteropServices.Marshal.FinalReleaseComObject(catalog);

相当于dispose();释放。

但是没有更好的解决方法。

在此记录一下,如果有人弄清楚了这个问题,麻烦留言告知。谢谢!

 

C# 压缩和修复Access数据库

标签:下载   led   obj   time   security   bsp   cti   indexof   字符串   

人气教程排行