用C#释放access数据库删除数据后的空间 access数据库删除数据后并不会自动释放空间,这样的话会占用很多的空间,能不能用代码释放这些空间,或者是通过设置数据库来让其自动释放空间。 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 using System.IO;using ADOX; //该命名空间包含创建ACCESS的类(方法)--解决方案 ==> 引用 ==> 添加引用 ==> 游览找到.dllusing JRO; //该命名空间包含压缩ACCESS的类(方法)public class Access{///根据指定的文件名称创建ACCESS数据库///mdbPath:要创件的ACCESS绝对路径public void Create(string mdbPath){if (File.Exists(mdbPath)) //检查数据库是否已存在{throw new Exception("目标数据库已存在,无法创建");}// 可以加上密码,这样创建后的数据库必须输入密码后才能打开mdbPath = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + mdbPath;// 创建一个CatalogClass对象的实例,ADOX.CatalogClass cat = new ADOX.CatalogClass();// 使用CatalogClass对象的Create方法创建ACCESS数据库cat.Create(mdbPath);}///压缩修复ACCESS数据库,mdbPath为数据库绝对路径public void Compact(string mdbPath){if (!File.Exists(mdbPath)) //检查数据库是否已存在{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 = mdbPath.Substring(0, mdbPath.LastIndexOf("\\") + 1) + temp;//定义临时数据库的连接字符串temp2 = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + temp;//定义目标数据库的连接字符串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);}/// 备份数据库,mdb1,源数据库绝对路径; mdb2: 目标数据库绝对路径 public void Backup(string mdb1, string mdb2){if (!File.Exists(mdb1)){throw new Exception("源数据库不存在");}try{File.Copy(mdb1, mdb2, true);}catch (IOException ixp){throw new Exception(ixp.ToString());}}///恢复数据库,mdb1为备份数据库绝对路径,mdb2为当前数据库绝对路径public void Recover(string mdb1, string mdb2){if (!File.Exists(mdb1)){throw new Exception("备份数据库不存在");}try{File.Copy(mdb1, mdb2, true);}catch (IOException ixp){throw new Exception(ixp.ToString());}} public class PackCompAccess{ // Fields private static string newFileName = string.Empty; private static string newPath = string.Empty; private static string sourceFileName = string.Empty; private static string sourcePath = string.Empty; // Methods//备份access public static bool BackupAccess(string sourcepath, string newpath) { sourcePath = sourcepath; newPath = newpath; if (!File.Exists(sourcePath)) { return false; } try { File.Move(sourcePath, newPath); return true; } catch { return false; } }//压缩access public static bool CompressAccess(string sourcepath, string newpath) { bool flag; sourcePath = sourcepath; newPath = newpath; sourceFileName = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + sourcePath; newFileName = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + newPath; if (!File.Exists(sourcePath)) { return false; } try { new JetEngineClass().CompactDatabase(sourceFileName, newFileName); File.Delete(sourcePath); File.Move(newPath, sourcePath); flag = true; } catch { flag = false; } return flag; }} 搞定啦!!!!!!!!谢谢楼上的不过有个地方要改动JRO.JetEngineClass jt = new JRO.JetEngineClass(); 改为:JRO.JetEngine jt = new JRO.JetEngine(); http://www.cnblogs.com/wuenping/archive/2009/07/27/1575965.html http://img.my.csdn.net/uploads/201211/23/1353654794_5117.jpg.thumb.jpg 求C#解析一段字符串的最佳方法 wpf 关于page与window vs2010 进行winforms开发时,是不是,还有一些问题。 C#窗体变形 关于日志:是记录在日志文件?还是记录在数据库表里?如何取舍? 求助,winform調用webservice 有私活呀,要求ASP.NET (C#),SqlServer2000数据库,做一个经销商管理系统. 大家积极交流啊!该如何获取一个小小网站的源代码呢?? 往ArrayList里添加数据的化,这些数据的排列顺序会自己变化吗? 求教一个关于分布式程序设计的问题! 在写网络爬虫,无法完成递归抓取,求大神解救! 如何定义这样的接口
using System.IO;
using ADOX; //该命名空间包含创建ACCESS的类(方法)--解决方案 ==> 引用 ==> 添加引用 ==> 游览找到.dll
using JRO; //该命名空间包含压缩ACCESS的类(方法)public class Access
{
///根据指定的文件名称创建ACCESS数据库
///mdbPath:要创件的ACCESS绝对路径
public void Create(string mdbPath)
{
if (File.Exists(mdbPath)) //检查数据库是否已存在
{
throw new Exception("目标数据库已存在,无法创建");
}
// 可以加上密码,这样创建后的数据库必须输入密码后才能打开
mdbPath = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + mdbPath;
// 创建一个CatalogClass对象的实例,
ADOX.CatalogClass cat = new ADOX.CatalogClass();
// 使用CatalogClass对象的Create方法创建ACCESS数据库
cat.Create(mdbPath);
}///压缩修复ACCESS数据库,mdbPath为数据库绝对路径
public void Compact(string mdbPath)
{
if (!File.Exists(mdbPath)) //检查数据库是否已存在
{
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 = mdbPath.Substring(0, mdbPath.LastIndexOf("\\") + 1) + temp;
//定义临时数据库的连接字符串
temp2 = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + temp;
//定义目标数据库的连接字符串
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);
}/// 备份数据库,mdb1,源数据库绝对路径; mdb2: 目标数据库绝对路径
public void Backup(string mdb1, string mdb2)
{
if (!File.Exists(mdb1))
{
throw new Exception("源数据库不存在");
}
try
{
File.Copy(mdb1, mdb2, true);
}
catch (IOException ixp)
{
throw new Exception(ixp.ToString());
}
}///恢复数据库,mdb1为备份数据库绝对路径,mdb2为当前数据库绝对路径
public void Recover(string mdb1, string mdb2)
{
if (!File.Exists(mdb1))
{
throw new Exception("备份数据库不存在");
}
try
{
File.Copy(mdb1, mdb2, true);
}
catch (IOException ixp)
{
throw new Exception(ixp.ToString());
}
}
public class PackCompAccess
{
// Fields
private static string newFileName = string.Empty;
private static string newPath = string.Empty;
private static string sourceFileName = string.Empty;
private static string sourcePath = string.Empty; // Methods
//备份access
public static bool BackupAccess(string sourcepath, string newpath)
{
sourcePath = sourcepath;
newPath = newpath;
if (!File.Exists(sourcePath))
{
return false;
}
try
{
File.Move(sourcePath, newPath);
return true;
}
catch
{
return false;
}
}
//压缩access
public static bool CompressAccess(string sourcepath, string newpath)
{
bool flag;
sourcePath = sourcepath;
newPath = newpath;
sourceFileName = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + sourcePath;
newFileName = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + newPath;
if (!File.Exists(sourcePath))
{
return false;
}
try
{
new JetEngineClass().CompactDatabase(sourceFileName, newFileName);
File.Delete(sourcePath);
File.Move(newPath, sourcePath);
flag = true;
}
catch
{
flag = false;
}
return flag;
}
}
谢谢楼上的
不过有个地方要改动
JRO.JetEngineClass jt = new JRO.JetEngineClass();
改为:JRO.JetEngine jt = new JRO.JetEngine();