When using VC, VB, Delphi, and other advanced languages to write database applications, you often need to configure ODBC data sources on the control panel. It may be difficult for users to configure ODBC data sources. Moreover, in practical applications, users often require access to different data sources in the same application. Therefore, the general loading method has insurmountable defects. To facilitate the use of applications, this article uses VC as the development environment to introduce two methods for dynamic loading of ODBC system data sources in applications. Method 1: Modify the registry design idea. Generally, after you configure the ODBC data source in the control panel, in Windows, some sub-keys are added to the Registry to store user configuration results. When the application needs to use the data source, Windows notifies the underlying interface to check the configuration of the data source in the registry. If you delete an ODBC data source, it will also respond in the registry. If the configured data source is a user data source, Windows will modify the HKEY_CURRENT_USER/software/ODBC in the registry. INI subkey; if the configured data source is a system data source, Windows will modify the HKEY_LOCAL_MACHINE/software/ODBC in the registry. INI primary key. Therefore, we can use the registry editing function in the Windows API in the application to complete windows work, so that we can dynamically load the data source. For different types of data sources, the Registry must be modified. One is in ODBC. create a sub-key with the same name as the data source description under the INI sub-key, and create items related to the data source configuration under the sub-key; the other is in the/ODBC. create a new item under the INI/ODBC Data Sources subkey to inform the driver manager of the ODBC data source type. The following code uses the configuration of a Microsoft Access Data source as an example to describe the function to implement this function. /* Strsourcename is the name of the data source to be created, strsourcedb is the database storage path, and strdescription is the description string of the data source. */Bool cloadodbcdlg: loaddbsource (cstring strsourcename, cstring strsourcedb, cstring strdescription) {// store the opened registry key hkey; DWORD dw; // store the return value of the registry API function execution long lreturn; // store the sub-key cstring strsubkey to open; // check whether the MS access ODBC driver is installed: odbcjt32.dll // obtain the Windows System directory char Sysdir [max_path]; char drvname [] = "// odbcjt32.dll";: getsystemdirectory (Sysdir, max_path); strcat (Sysdir, drvname ); cfilefind find File; If (! Findfile. findfile (Sysdir) {afxmessagebox ("your computer system does not have the ODBC driver odbcjt32.dll with MS access installed, and you will not be able to load this type of data source. ", Mb_ OK | mb_iconstop); Return false;} strsubkey =" software // ODBC. INI // "+ strsourcename; // create the sub-key lreturn =: regcreatekeyex (hkey_local _ machine, (lpctstr) strsubkey, 0, null, reg_option _ non_volatile, key_write, null, & hkey, & DW); If (lreturn! = Error_success) return false; // set the parameters of the data source 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), strdbq. getlength ();: regsetvalueex (hkey, "Description", 0l, REG_SZ, (const byte *) (LP Ctstr) strdescription), strdescription. getlength ();: regsetvalueex (hkey, "driver", 0l, REG_SZ, (const byte *) (lpctstr) strdriver), strdriver. getlength ();: regsetvalueex (hkey, "driverid", 0l, REG_DWORD, (const byte *) (& dwdriverid), sizeof (DW ));:: regsetvalueex (hkey, "fil", 0l, REG_SZ, (const byte *) (lpctstr) strfil), strfil. getlength ();: regsetvalueex (hkey, "PWD", 0l, REG_SZ, (const byte *) (L Pctstr) strpwd), strpwd. getlength ();: regsetvalueex (hkey, "safetransactions", 0l, REG_DWORD, (const byte *) (& dwsafetransactions), sizeof (DW ));:: regsetvalueex (hkey, "uid", 0l, REG_SZ, (const byte *) (lpctstr) struid), struid. getlength ();: regclosekey (hkey); // create the ODBC Data Source's jet subkey strsubkey + = "// engines // Jet"; lreturn = :: regcreatekeyex (HKEY_LOCAL_MACHINE, (lpctstr) strsubkey, 0, null, reg_option_non _ Volatile, key_write, null, & hkey, & DW); If (lreturn! = Error_success) return false; // set the parameters cstring strimplict = ""; cstring strusercommit = "yes"; DWORD dwpagetimeout = 5; DWORD dwthreads = 3; DWORD dwmaxbuffersize = 2048;: regsetvalueex (hkey, "implictcommitsync", 0l, REG_SZ, (const byte *) (lpctstr) strimplict), strimplict. getlength () + 1);: regsetvalueex (hkey, "maxbuffersize", 0l, REG_DWORD, (const byte *) (& dwmaxbuffersize), sizeof (DW);: regsetval Ueex (hkey, "pagetimeout", 0l, REG_DWORD, (const byte *) (& dwpagetimeout), sizeof (DW);: regsetvalueex (hkey, "Threads", 0l, REG_DWORD, (const byte *) (& dwthreads), sizeof (DW);: regsetvalueex (hkey, "usercommitsync", 0l, REG_SZ, (const byte *) (lpctstr) strusercommit), strusercommit. getlength ();: regclosekey (hkey); // set the ODBC database engine name lreturn =: regopenkeyex (HKEY_LOCAL_MACHINE, "software // ODBC. in I // ODBC Data Sources ", 0l, key_write, & hkey); If (lreturn! = Error_success) return false; cstring strdbtype = "Microsoft Access Driver (*. MDB) ";: regsetvalueex (hkey, strsourcename, 0l, REG_SZ, (const byte *) (lctstr) strdbtype), strdbtype. getlength (); Return true;} Since dynamic loading only changes database files, data source descriptions, and data source descriptions, the above functions can meet most of the requirements in applications. If more changes are required in the application, you can also change the function parameters. You can use overload functions with different parameters to dynamically load multiple types of data sources. Method 2: The Dynamic Link Library odbcinst. dll in the Windows system subdirectory provides a function sqlconfigdatasource () that can dynamically add, 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 the parent window handle. If the value is null, the dialog box related to the parent window is not displayed. The frequest parameter can be set to one of the following values: odbc_add_dsn: Add a new user data source; odbc_config_dsn: Modify (configure) an existing user data source; odbc_remove_dsn: delete an existing user data source; odbc_add_sys_dsn: Add a new system data source; odbc_config_sys_dsn: Modify (configure) an existing system data source; odbc_remove_sys_dsn: delete an existing system data source. The lpszdriver parameter is used to pass the database engine name, which is equivalent to the strdbtype variable in method 1. The lpszattirbutes parameter is the value of a keyword, that is, a series of "keyname = value" strings separated, for example, DSN = Personnel Data/0uid = Smith/0 database = personnel. For detailed settings of this parameter, see the help documentation of sqlconfigdatasource () function and various ODBC driver documentation in msdn. The specific implementation is because the default library file of VC does not contain the sqlconfigdatasource () function, so before using this function, you need to set the odbcinst. the H file is included in the header file of the project. In the settings Properties dialog box of the project, add odbc32.lib to the object/library modules edit box on the Link property page, and ensure that the file odbccp32.dll is under system32. Take Microsoft Access as an example. Set the data source name to demo and the data source description to "Sample Data Source". Then add the following code to the place where the data source needs to be dynamically loaded: sqlconfigdatasource (null, odbc_add_sys_dsn, "Microsoft Access Driver (*. MDB) "," DSN = demo/0 descirption = Sample Database "); summary the above two methods can dynamically load various types of ODBC data sources, in the Windows 95/98/NT/2000 environment, debugging is successful. Method 1 requires a lot of code for implementation. method 2 requires less code, but requires support for additional files. As the flexibility of data source configuration increases, in order to form the lpszattributes string, the code length is also increased. Since the data source is configured on the control panel, the programmer can get a more intuitive understanding of the values in the Registry and the corresponding item names, in addition to reading the relevant driver documentation, programmers can configure the ODBC data source through the control panel before programming, and then program according to the corresponding part of the Registry.
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.