服务热线
153 8323 9821
/// MBD compact method (c) 2004 Alexander Youmashev
/// !!IMPORTANT!!
/// !make sure there's no open connections
/// to your db before calling this method!
/// !!IMPORTANT!!
///
/// connection string to your db
/// FULL name
/// of an MDB file you want to compress.
public static void CompactAccessDB(string connectionString, string mdwfilename)
{
object[] oParams;
//create an inctance of a Jet Replication Object
object objJRO =
Activator.CreateInstance(Type.GetTypeFromProgID("JRO.JetEngine"));
//filling Parameters array
//cnahge "Jet OLEDB:Engine Type=5" to an appropriate value
// or leave it as is if you db is JET4X format (access 2000,2002)
//(yes, jetengine5 is for JET4X, no misprint here)
oParams = new object[] {
connectionString,
"Provider=Microsoft.Jet.OLEDB.4.0;Data" +
" Source=C:\\tempdb.mdb;Jet OLEDB:Engine Type=5"};
//invoke a CompactDatabase method of a JRO object
//pass Parameters array
objJRO.GetType().InvokeMember("CompactDatabase",
System.Reflection.BindingFlags.InvokeMethod,
null,
objJRO,
oParams);
//database is compacted now
//to a new file C:\\tempdb.mdw
//let's copy it over an old one and delete it
System.IO.File.Delete(mdwfilename);
System.IO.File.Move("C:\\tempdb.mdb", mdwfilename);
//clean up (just in case)
System.Runtime.InteropServices.Marshal.ReleaseComObject(objJRO);
objJRO = null;
}
using System;
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("目标数据库已存在,无法创建");
}
// 可以加上密码,这样创建后的数据库必须输入密码后才能打开
上一篇:SEO的关键词密度
下一篇:access iif语句