1. Add password to database without password:
hr = m_pConnection->Open("Provider=Microsoft.Jet.OLEDB.4.0;User ID=Admin;Data Source=d:\\DB\\REMO.mdb;Mode=Share Deny Read|Share Deny Write", "","",0);
m_pConnection->Execute("ALTER DATABASE PASSWORD [123] NULL",NULL,adCmdText);
The above is set password to 123
2. Change the password 123 to 456:
hr = m_pConnection->Open("Provider=Microsoft.Jet.OLEDB.4.0;User ID=Admin;Data Source=d:\\DB\\REMO.mdb;Jet OLEDB:DataBase Password=SANYCOM;Mode=Share Deny Read|Share Deny Write","","",0);
m_pConnection->Execute("ALTER DATABASE PASSWORD [456] [123]",NULL,adCmdText);
Notice the difference from 1: the first parameter of Open has a password section so that the database can be developed properly.
Note: The data in the first parameter in open source= full path \ filename. mdb (no full path open failed)
3, access to the database with the password
Method One:
hr = m_pConnection->Open("DSN=REMO","Admin","456",0);
//其中REMO为数据源,合适数据源存在的情况,当然如果open失败可以重新设置数据源(见后面)
Method Two:
hr = m_pConnection->Open("Provider=Microsoft.Jet.OLEDB.4.0;User ID=Admin;Data Source=d:\\DB\\REMO.mdb;Jet OLEDB:DataBase Password=456;Mode=Share Deny Read|Share Deny Write","","",0);
Note: 2nd, 3 parameters are empty, the parameter value is already in the first parameter, the disadvantage: the data source location in the program is written dead, not good, the best dynamic access to the database file location, with GetModuleFileName () very convenient.
4. How to set up the data source in the program:
Set the location of the database files in the previous directory of SMC
//-----------------------------------------------------------
CString spath;
Get the full path and program name of the executable program
GetModuleFileName (Null,spath.getbuffersetlength (max_path+1), MAX_PATH);
Spath.releasebuffer ();
AfxMessageBox (spath);
int NPOs;
Npos=spath.reversefind (' \ \ ');
Spath=spath.left (NPOs);
Npos=spath.reversefind (' \ \ ');
Spath=spath.left (NPOs);
Get the path and filename of the database file
CString lpszfile = spath + "\\REMO.mdb";
M_strfilepath = lpszfile;
char* Szdesc;
int Mlen;
Szdesc=new char[256];
sprintf (Szdesc, "dsn=%s?") description= remote onboard control data source? Dbq=%s? Fil=microsoftaccess? defaultdir=%s?? "," REMO ", Lpszfile,spath);
AfxMessageBox (SZDESC);
Mlen = strlen (SZDESC);
for (int i=0; i<mlen; i++)
{
if (szdesc[i] = = '? ')
Szdesc[i] = ' the ';
}
AfxMessageBox (SZDESC);
if (FALSE = = SQLConfigDataSource (null,odbc_add_sys_dsn, Microsoft Access Driver (*.mdb), (LPTSTR) szdesc))
{
MessageBox ("Create data source failed, program will exit.") Please set the data source manually. "," warns the mb_iconhand|. mb_iconstop| MB_ICONERROR);
---2004.6.7---add--start--//
Eject the ODBC Configuration window to set
Char Cwinpath[max_path];
GetWindowsDirectory (Cwinpath,max_path);
CString Strwinpath = (CString) Cwinpath + "\\system32\\odbcad32.exe";
Try{winexec (strwinpath,1);}
catch (_com_error e)
{
AfxMessageBox (E.description ());
}
---2004.6.7---add---end-//
PostQuitMessage (0);
}
Delete Szdesc;
Configure the data source function SQLConfigDataSource () requires #include <odbcinst.h> support.
Also, add ODBCCP32.lib library files