修改Access数据库密码,弹出“密码错误”提示
使用C++ Builder修改Access数据库密码,为什么总是弹出密码错误?
void __fastcall TForm1::btn_SetPwdClick(TObject *Sender){ this->ADOConnection1->Close(); this->ADOConnection1->ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;\ Data Source=C:\\Program Files\\Borland\\CBuilder6\\Projects\\ADO_rev_db\\AccoutMe.mdb;\ Mode=Share Deny Read|Share Deny Write;\ Persist Security Info=False"; this->ADOConnection1->LoginPrompt = false; try { this->ADOQuery1->Connection = this->ADOConnection1; this->ADOQuery1->Close(); this->ADOQuery1->SQL->Clear(); //更改密码的SQL代码如何写? //this->ADOQuery1->SQL->Text = " alter database password '(null)' 'abc' "; //测试 this->ADOQuery1->SQL->Text = String().sprintf( "alter database password '%s' '%s'", edt_OldDBPWD->Text.Trim(), edt_NewDBPWD->Text.Trim() ); this->ADOConnection1->Open(); this->ADOQuery1->ExecSQL(); ShowMessage("数据库密码修改成功,请使用新密码"); } catch(...) { throw("ERROR"); } }
BOOL __stdcall CrnCompactMDB(LPCSTR lpMdbFile, LPCSTR lpOldPwd, LPCSTR lpNewPwd){ char szPath[MAX_PATH] = { 0 }; ::GetTempPathA(MAX_PATH, szPath); char szTmpFile[MAX_PATH] = { 0 }; ::GetTempFileNameA(szPath, "ccm", 0, szTmpFile); char szOldProvide[512] = { 0 }, szNewProvide[512] = { 0 }; sprintf(szOldProvide, "Provider=Microsoft.Jet.OLEDB.4.0;" "Data Source=%s;" "Jet OLEDB:Database Password=%s", lpMdbFile, lpOldPwd); sprintf(szNewProvide, "Provider=Microsoft.Jet.OLEDB.4.0;" "Data Source=%s;" "Jet OLEDB:Database Password=%s", szTmpFile, lpNewPwd); BOOL bSuccess = FALSE; Variant vAdoObj; try { if (::PathFileExistsA(szTmpFile)) ::DeleteFileA(szTmpFile); vAdoObj = Variant::CreateObject("JRO.JetEngine"); vAdoObj.OleProcedure("CompactDatabase", WideString(szOldProvide), WideString(szNewProvide)); if (::PathFileExistsA(lpMdbFile)) ::DeleteFileA(lpMdbFile); ::MoveFileA(szTmpFile, lpMdbFile); bSuccess = TRUE; } __finally { vAdoObj.Clear(); vAdoObj = Unassigned; } return bSuccess;}