午夜国产狂喷潮在线观看|国产AⅤ精品一区二区久久|中文字幕AV中文字幕|国产看片高清在线

    用c#壓縮和修復(fù)access數(shù)據(jù)庫
    來源:易賢網(wǎng) 閱讀:1040 次 日期:2014-10-21 14:47:20
    溫馨提示:易賢網(wǎng)小編為您整理了“用c#壓縮和修復(fù)access數(shù)據(jù)庫”,方便廣大網(wǎng)友查閱!

    介紹

    下面這段c# 代碼可以用來壓縮和修復(fù)access數(shù)據(jù)庫,不管它是一個簡單的.mdbaccess數(shù)據(jù)庫還是一個.mdw網(wǎng)絡(luò)共享數(shù)據(jù)庫,這個過程和你在用ms access應(yīng)用程序中使用的工具-數(shù)據(jù)庫實用工具-壓縮和修復(fù)時執(zhí)行的操作完全一樣.實例代碼使用了遲綁定(運行中在內(nèi)存中建立com對象),這樣就不需要在工程中加入com引用了,也不需要在pc上安裝ms access應(yīng)用程序.只需要一個jet引擎(jet引擎包含在mdac安裝包中,在windows nt4以后的版本中,系統(tǒng)已經(jīng)自帶了這個引擎).

    c#操作access數(shù)據(jù)庫的背景

    不知你是否也厭煩了在工程中加入復(fù)雜的com庫引用,但我相信這個純.net代碼將省去額外的交互操作, rcws和com引用.基本上,由于系統(tǒng)中安裝的microsoft類庫的不同(例如:ms office object library 9,10,11等等),我們也不知道用戶pc中安裝的office版本,所以我們要通過progid來訪問com對象,而不能用clsid.例如,當(dāng)調(diào)用excel.application,時,得到的是excel,而不管系統(tǒng)中安裝ms office的版本,當(dāng)在代碼中加入ms excel 10 object library引用時,其實只是給應(yīng)用程序加入了一個非常受限制的功能.所以我們使用system.reflection和遲綁定.

    1. 實例代碼

    只需調(diào)用compactaccessdb函數(shù)即可壓縮和修復(fù)目標(biāo)數(shù)據(jù)庫.

    2. 參數(shù):

    connectionstring – 用來連接到access數(shù)據(jù)庫.

    mdwfilename –要壓縮的mdb文件的全名(路徑+文件名).

    由于jet引擎的限制,執(zhí)行此方法壓縮access數(shù)據(jù)庫會把結(jié)果生成為一個新文件,所以我們要還需要把這個新的access文件拷貝到目的位置覆蓋原來未壓縮文件.

    當(dāng)調(diào)用此方法時請確認(rèn)被壓縮數(shù)據(jù)庫無打開的連接.

    /**//// 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;

    }

    更多信息請查看IT技術(shù)專欄

    更多信息請查看數(shù)據(jù)庫
    易賢網(wǎng)手機(jī)網(wǎng)站地址:用c#壓縮和修復(fù)access數(shù)據(jù)庫
    由于各方面情況的不斷調(diào)整與變化,易賢網(wǎng)提供的所有考試信息和咨詢回復(fù)僅供參考,敬請考生以權(quán)威部門公布的正式信息和咨詢?yōu)闇?zhǔn)!

    2025國考·省考課程試聽報名

    • 報班類型
    • 姓名
    • 手機(jī)號
    • 驗證碼
    關(guān)于我們 | 聯(lián)系我們 | 人才招聘 | 網(wǎng)站聲明 | 網(wǎng)站幫助 | 非正式的簡要咨詢 | 簡要咨詢須知 | 新媒體/短視頻平臺 | 手機(jī)站點 | 投訴建議
    工業(yè)和信息化部備案號:滇ICP備2023014141號-1 云南省教育廳備案號:云教ICP備0901021 滇公網(wǎng)安備53010202001879號 人力資源服務(wù)許可證:(云)人服證字(2023)第0102001523號
    云南網(wǎng)警備案專用圖標(biāo)
    聯(lián)系電話:0871-65099533/13759567129 獲取招聘考試信息及咨詢關(guān)注公眾號:hfpxwx
    咨詢QQ:1093837350(9:00—18:00)版權(quán)所有:易賢網(wǎng)
    云南網(wǎng)警報警專用圖標(biāo)