I. Dynamic creation of ODBC data sources
The ODBC API provides a function SQLConfigDataSource to dynamically create a data source. The function is prototyped as follows:
BOOL SQLConfigDataSource (HWND hwndparent, WORD frequest, LPCSTR lpszdriver, LPCSTR lpszattributes);
The parameters are described as follows:
(1)hwndparent
Specifies the parent window handle, which you can specify as NULL when you do not need to create a Data Source dialog box.
(2)frequest
Specify the operation contents of the function, with the following values:
ODBC_ADD_DSN: Add a new user data source;
ODBC_CONFIG_DSN: Modify an existing user data source;
ODBC_REMOVE_DSN: In addition to an existing user data source;
odbc_add_sys_dsn: Add a new system data source;
ODBC_CONFIG_SYS_DSN: Configure or modify an existing system data source;
ODBC_REMOVE_SYS_DSN: Delete an existing system data source;
ODBC_REMOVE_DEFAULT_DSN: Deletes the missing data source Description section.
(3)Lpszdriver
Specifies the ODBC data engine name. For example: Microsoft Access Driver (*.mdb), SQL server,microsoft ODBC for Oracle, and so on.
(4)lpszattributes
Specifies an ODBC data source property that contains a list of key-values that are necessary to configure the data source. Some of the keywords are described below:
DSN: Data source name;
FILEDSN: File data source name;
DRIVER: Data-driven descriptions, such as SQL Server, Oracle, and so on.
UID: User ID;
Pwd:uid the corresponding password, if there is no password, can also be an empty string;
SAVEFILE: Saves the name of the. dsn file that maintains the value of the keyword attribute for the current successful connection.
Database: Names of databases;
DBQ: Absolute path to the database file (including file name);
DefaultDir: Default database file path (does not include file name);
Attention:
1) If the information provided by Lpszattributes is not complete enough, a dialog box will appear during the creation process to require the user to provide the appropriate information.
2) Each of the two key-name values is separated by '/', and the last Key-name value is followed by two '.
3) Run Regedit to open the registry and turn Hkey_current_user->software->odbc->odbc on. INI, you can see the registered DSN, which can be modeled after the registered DSN attribute to set the lpszattributes.
4) The DSN name must be unique, and if the DSN to be registered has been registered, then SQLConfigDataSource modifies the properties of the original DSN.
Attention:
1. The odbcinst.h header file containing the system must be declared when using the SQLConfigDataSource function.
2, use the SQLConfigDataSource function must use the Odbccp32.dll, it is provided by Microsoft 32-bit ODBC installation and management of the DLL, if 16 bits must be used to Odbcinst.dll,odbccp32.dll There is an import library. The solution is to add the odbccp32.lib to our project, we can open project system menu item, select Add to Project submenu, choose Files item in it, open VC install directory \vc\lib\ directory, file type Select Library Files (. lib), select the Odbccp32.lib, and then press OK. The ODBCCP32.DLL is also placed in the system subdirectory.
Examples are as follows:
CString strdir;::getcurrentdirectory (MAX_PATH, Strdir.getbuffer (MAX_PATH+1) ; Strdir.releasebuffer (); TCHAR Sztemppath[max_path+1] = {' /'};sprintf (Sztemppath,"dsn=%s! dbq=%s! defaultdir=%s!!", CString ("SOWM"), Strdir+ CString ("\\SOWM.mdb"), strdir);intSztemppathlen =strlen (sztemppath); for(inti =0; i < Sztemppathlen; ++i) {if(Sztemppath[i] = ='!') Sztemppath[i]=' /';}if(!SQLConfigDataSource (NULL, ODBC_ADD_SYS_DSN,"Microsoft Access Driver (*.mdb)", (LPCSTR) Sztemppath)) {AfxMessageBox ("Configuring an ODBC data source failed!"); returnFALSE;}
If you want to access files on your local area network, Lpszattributes can join: Data SOURCE=\\IP address \ \ shared Folder \\SOWM.mdb.
Second, connect the data source
Examples are as follows:
//whether the test is in an open stateif(!M_database.isopen ()) {TRY {if(!m_database.openex (_t ("SOWM") , Cdatabase::noodbcdialog) AfxMessageBox ("Database opened failed.\n"); } CATCH (CMemoryException, E) {TRACE ("Database opened Memory Exception occur.\n"); } CATCH (CDBException, E) {TRACE ("Database opened DB Exception occur:%s\n", e->m_strerror); } End_catch}
In the destructor, add:
if (M_database.isopen ()) m_database.close ();
Note: include # include <afxdb.h> in stdafx.h to support database operations.
MFC ODBC (i)