The method of dynamic loading ODBC in VC is written when writing database applications using high-class languages such as VC, VB, Delphi, often requires users themselves to configure ODBC data sources in the control panel. For general users, configuring ODBC data sources may be a relatively difficult job. Moreover, in practical applications, users tend to access different data sources in the same application, so the general loading method has a defect that cannot be overcome. In order to complete this work in the program, it is convenient for the application of the application. This article describes two ways to dynamically load an ODBC system data source in the application in VC. Method 1: Modifying the Registry Design Idea Generally, when the user configures the ODBC data source in the control panel, the Windows system adds some sub-keys to the registry to store the user's configuration. When the application needs to use the data source, Windows will notify the underlying interface to see the configuration of the data source in the registry. If the user deletes an ODBC data source, it will also react in the registry. If the configured data source is a user data source, the Windows system will modify the hkey_current_user / software / uf键 / ODBC.ini subkey of the registry; if the configuration of the data source is a system data source, the Windows system will modify the hkey_local_machine / Software / ODBC / ODBC.INI master key. Therefore, we can use the registry editing function in the Windows API to complete the work made by the registry in the application, which can achieve the purpose of dynamically loaded data sources. Specific implementation for different types of data sources, the modifications of the registry are also different, but basically modify two places. One is a sub-key that is the same name with the data source description name under the ODBC.ini sub-key and establishes the item related to the data source configuration under the subkey; the other is under the odbc.ini / odbc data sources subkey Establish a new item to tell the driver manager ODBC data source. The following is to configure a Microsoft Access data source to give a code that implements this function. / * STRSourceName is the data source to create, and strsourceDB is a data stock path, and StRDESCRIption is a description string of the data source. * / BOOL Cloadodbcdlg :: LoadDbsource (CString strsourceName, cstring strsourcedb, cstring strdescription) {// Storage Open registry key HKEY HKEY; DWORD DW; / / Store Registry API function execution return value long lreturn; // Store Open subkey cstring strsubkey; // detects whether the MS Access ODBC Driver: ODBCJT32.DLL // Gets Windows System Directory Char sysdir [MAX_PATH]; char DRVNAME [] = "// odbcjt32.dll"; :: getSystemDirectory sysdir, max_path; strcat (sysdir, drvname); cfilefind findfile; if (! FindFile.FindFile (sysdir)) {AFXMessageBox ("Your computer system ODBC driver ODBCJT32.DLL installed in your computer system, you will not be loaded This type of data source.
", MB_OK | MB_ICONSTOP); RETURN FALSE;} strsubkey =" Software // odbc // odbc.ini // " strsourceName; // Create an ODBC data source in the registry LRETURN = :: regReateKeyex (HKEY_LOCAL_MACHINE, (LPCTSTR) strSubKey, 0, NULL, REG_OPTION_NON_VOLATILE, KEY_WRITE, NULL, & hKey, & dw);! if (lReturn = ERROR_SUCCESS) return false; // set the data source parameters CString strDbq = strSourceDb; CString strDriver = sysDir; DWORD dwDriverId = 25; CString strFil = "MS Access;"; CString strPwd = strSourceName; DWORD dwSafeTransactions = 0; CString strUid = strSourceName; :: RegSetValueEx (hKey, "DBQ", 0L, REG_SZ, (CONST BYTE *) ((LPCTSTR ), strdbq.getLength ()); :: RegSetValueex (HKEY, "Description", 0L, REG_SZ, (Const Byte *) ((lpctstr) strdescription, strdescription.getlength ()); :: RegSetValueex (HKEY, "Driver", 0L, REG_SZ, (Const Byte *) ((lpctstr) striver, strDriver.getlength ()); :: RegSetValueex (HKEY, "Driverid", 0L, REG_DWORD, (Const Byte *) (& dwdriver), SizeOf (dw)); :: RegSetValueex (HKEY, "Fil", 0L, Reg_SZ, (const byte *) ((lpctstr) ST RFIL), strfil .getlength ()); :: RegSetValueex (HKEY, "PWD", 0L, REG_SZ, (Const Byte *) ((lpctstr) strpwd, strpwd.getLength ()); :: RegSetValueex (HKEY, " Safetransactions, 0L, Reg_dword, (Const Byte *), SizeOf (DW)); :: RegSetValueex (HKEY, "UID", 0L, REG_SZ, (Const Byte *) ((LPCTSTSTR) STRUID, STRUID. GetLength ()); :: regclosekey (HKEY); // Create an ODBC data source Jet sub-key strsubkey = "/////// jet"; Lreturn = :: regReateKeyex (HKEY_LOCAL_MACHINE, (LPCTSTSTR) strsubkey, 0, null , REG_OPTION_NON_VOLATILATION, Key_Write, Null, & HKey, & DW); if (Lreturn! =
ERROR_SUCCESS) return false; // set the parameters in the sub-key CString strImplict = ""; CString strUserCommit = "Yes"; DWORD dwPageTimeout = 5; DWORD dwThreads = 3; DWORD dwMaxBufferSize = 2048; :: RegSetValueEx (hKey, "ImplicitCommitSync", 0L, REG_SZ, (CONST BYTE *) ((LPCTSTR) strImplict), strImplict.GetLength () 1); :: RegSetValueEx (hKey, "MaxBufferSize", 0L, REG_DWORD, (CONST BYTE *) (& dwMaxBufferSize SizeOf (DW)); :: RegSetValueex (HKEY, "Pagetimeout", 0L, Reg_dword, (Const Byte *) (& DWPAGETIMEOUT), SIZEOF (DW)); :: RegSetValueex (HKEY, "Threads", 0L, REG_DWORD (& DWTHREADS), SIZEOF (DW)); :: RegSetValueex (HKEY, "UserCommitsync", 0L, REG_SZ, (Const Byte *) ((LPCTSTSTETE *) () StruserCommit.getlength ());::: : RegcloseKey (HKEY); // Set the ODBC database engine name Lreturn = :: regopenkeyex (HKEY_LOCAL_MACHINE, "Software // ODBC // ODBC.INI // ODBC DATA SOURCES", 0L, Key_Write, & HKey); if (Lreturn! = ERROR_SUCCESS) RETURN FALSE; CSTRING STRDBTYPE = "Microsoft Access Driver (* .mdb)"; :: RegSetValueex (HKEY, STRSOURENAME, 0L, REG _SZ, (Const Byte *) ((lpctstr) strdbtype, strdbtype.getlength ()); return true;} Due to dynamic loading, the database file, data source description, and data source description are generally changed, so the above functions can be Implement most of the requirements in the application. If more changes are required in the application, you can also be implemented by changing the function parameters. For conditions that need to be dynamically loaded multiple types of data sources, you can use the overload function with different parameters. Method 2: Using the DLL Design Idea Windows System subdirectory Dynamic Link Library ODBCINST.DLL provides a function of functionality () that can dynamically increase, modify, and delete data sources (). The prototype of this function is as follows: BOOL SQLCONFIGDATASOURCE (HWND HWNDPARENT, WORD FREQUEST, LPCSTR LPSZDRIVER, LPCSTR LPSZATTRIBUTES); HWndParent parameter is a parent window handle. If this value is NULL, the dialog related to the parent window will not appear.