WEB开发网
开发学院软件开发C语言 C#操作ACCESS数据库(创建,压缩,备份,恢复) 阅读

C#操作ACCESS数据库(创建,压缩,备份,恢复)

 2009-04-20 08:26:30 来源:WEB开发网   
核心提示: 当调用此方法时请确认被压缩数据库无打开的连接.C#操作ACCESS数据库 代码如下:/// MBD compact method © 2004 Alexander Youmashev/// !!IMPORTANT!!/// !make sure there's no op

当调用此方法时请确认被压缩数据库无打开的连接.

C#操作ACCESS数据库 代码如下:

/// MBD compact method © 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;
}

Tags:操作 ACCESS 数据库

编辑录入:爽爽 [复制链接] [打 印]
赞助商链接