Home >Database >Mysql Tutorial >C++ Builder修改Access数据库密码

C++ Builder修改Access数据库密码

WBOY
WBOYOriginal
2016-06-07 15:31:591284browse

void __fastcall TForm1::Button1Click(TObject *Sender){ AnsiString f1= AccoutMe.mdb ; // 源库1 AnsiString psw1= 123456; // 密码 1,若设置错误也会报“意外错误” AnsiString f2= AccoutMe_new.mdb ; // 源库2 AnsiString psw2= abc; // 新 密码 2 bo

void __fastcall TForm1::Button1Click(TObject *Sender)
{
    AnsiString   f1= "AccoutMe.mdb ";           //   源库1
    AnsiString   psw1= "123456";                //   <strong>密码</strong>1,若设置错误也会报“意外错误”
    AnsiString   f2= "AccoutMe_new.mdb ";       //   源库2
    AnsiString   psw2= "abc";                   //   新<strong>密码</strong>2
    bool flg_Success = false;

    AnsiString   dir = ExtractFilePath(Application-> ExeName);
    f1=dir+f1;                              //新<strong>数据库</strong>绝对路径
    f2=dir+f2;

    if (FileExists( f2 ) )      //若目标文件已存在,先删除
            DeleteFileA(f2);

    char Provider1[512] = { 0 }, Provider2[512] = { 0 };

    sprintf(Provider1,
            "Provider=Microsoft.Jet.OLEDB.4.0;"
             "Data Source='%s';"
             "Jet OLEDB:Database Password='%s'" , f1, psw1
           );

//    sprintf(Provider2,
//            "Provider=Microsoft.Jet.4.0.OLE DB.Provider;"
//            "Data Source='%s';"
//            "Jet OLEDB:Database Password='%s'"
//            , f2, psw2
//            );   // 注意提供正确的Provider写法,否则提示“意外错误”

    sprintf(Provider2,
            "Provider=Microsoft.Jet.OLEDB.4.0;"
            "Data Source='%s';"
            "Jet OLEDB:Database Password='%s'" , f2, psw2
           );

    Variant Adoobj = Variant::CreateObject( "JRO.JetEngine");
    
    try
    {
        Adoobj.OleProcedure( "CompactDatabase", WideString(Provider1), WideString(Provider2) );

        if (FileExists(f1))
            DeleteFileA(f1);

        RenameFile(f2, f1);

        flg_Success = true;
    }
    __finally
    {
        Adoobj.Clear();
        Adoobj = Unassigned;
    }
    ShowMessage( BoolToStr(flg_Success, true) ) ;

}


这是OLE的方法,由于代码较简洁就不多写注释了。

原帖子见:http://topic.csdn.net/u/20120214/17/6bde8f44-3afb-4f8d-afc1-f971c879e3d9.html 

特别感谢老妖!

 

PS:

OLE报错太不友好了。

不论是用独占方式打开ACCESS用SQL语句ALTER DATABASE PASSWORD "旧密码"   "新密码",

还是用ADOQuery等控件,设置独占打开方式和 ADO 控件的SQL属性为"alter…………"修改ACCESS密码,都没有成功。

调试中ing...

 

 

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn