Use Visual C ++ to set ODBC Data sources

Source: Internet
Author: User
Tags driver manager dsn

ODBC (Open Database conectivity) is the interconnection of open databases. As an important part of the windows open structure, it is already familiar to many windows programmers. ODBC depends on the drivers provided by database manufacturers, when using ODBC APIs, the Windows ODBC management program passes database access requests to the correct driver, and the driver then uses SQL statements to instruct the DBMS to complete database access. Therefore, the existence of ODBC provides us with powerful capabilities and flexibility to develop application database programs.

---- To enable ODBC to work with the database, you must register the database with the ODBC driver manager. This can be done by defining a DSN or data source name. Generally, we can only manually open the system control panel, run the ODBC Data Source manager, and manually configure the data source. However, this work is too complicated for users, we must consider using a program to complete these configurations for users.

---- Therefore, many programmers hope to have an excellent installer to automatically set the ODBC data source when releasing their own database software, although some excellent installation and production software such as InstallShield can help us implement such functions, after all, it lacks flexibility and programmers cannot fully control it. In fact, we can write some programs to implement such functions by ourselves. There are several methods to achieve this. One method is to use a program to modify the Windows registry, programmers can use the Windows API function to add and modify ODBC under HKEY_LOCAL_MACHINE \ SOFTWARE \ ODBC. this method is cumbersome. I recommend using ODBC in a program.
API method. programmers can call these API functions to set ODBC Data Sources at any time using programs written in Visual C ++.

---- Below I will use MFC to write a program to demonstrate how to implement this function:

---- First, open visual c ++, select new from the File menu, and then select the project of the MFC Appwizard (exe) class. The project name is set to try, and press OK, select dialog based in the Next Step 1 screen. Because you do not need to use the following options, press the finish key and the system will generate a new project. After completing the above work, in the left-side workspace window, select resourceview to open the dialog resource in try resources, select and open the idd_try_dialog dialog window, and click the button icon in the controls menu window, return to the idd_try_dialog dialog window and click this window. A button named button1 is generated. Select this button, right-click it, and select Properties from the pop-up menu, in the displayed dialog box, change the value of button1 of the caption item to setup.
ODBC: close this dialog box. Right-click this button and select classwizard. In the displayed dialog box, double-click bn_clicked in idc_button1 and messages for object IDs, the add member function dialog box is displayed. The member function name is onbutton1 and press OK. Double-click onbutton1 on_idc_buttion1: bn_clicked in the member functions option. Replace the following odbc api statement with the void ctrydlg: onbutton1 () function.
// Todo: add your control notification handler code here:

  SQLConfigDataSource(NULL,ODBC_ADD_SYS_DSN,"Microsoft Access Driver (*.mdb)\0","DSN=TryDB\0DBQ=D:\\Database\\try.mdb\0DEFAULTDIR=D:\\DATABASE\0\0");  '

---- You can modify the preceding statement according to your different settings. sqlconfigdatasource generally has the following license parameters: odbc_add_dsn: Add a new user data source, odbc_config_dsn: modify 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 an existing system data source, odbc_remove_sys_dsn:
Delete an existing system data source, odbc_remove_default_dsn: Delete the description section of the missing data source. Note that when using the sqlconfigdatasource odbc api function, you must declare the odbcinst that contains the system. h header file, so we can select FileView In the workspace window to open try in header files. h. Add # include "odbcinst. H ". If this header file is not added, the system will display the undeclared identifier error during compilation. After completing the preceding steps, if we compile and link this project immediately, the following error occurs:

tryDlg.obj : error LNK2001: unresolvedexternal symbol _SQLConfigDataSource@16Debug/try.exe : fatal error LNK1120: 1 unresolved externals

---- Some people may give up because they cannot find an error. In fact, this is because odbccp32.dll must be used when using the sqlconfigdatasource API function, it is a 32-bit ODBC installation and management DLL provided by Microsoft. If it is a 16-bit, it must use odbcinst. DLL, odbccp32.dll has an import library, so the solution is to add this odbccp32.lib to our project. We can open the project system menu item and select Add
Project Sub-menu, select files, open \ Vc \ Lib \ directory under the VC installation directory, select library files (. lib), select odbccp32.lib, press OK, and re-compile to run the program. The dialog window will pop up, press the "setup ODBC" button, and then, you can view the running result through the ODBC data source Manager or registry on the control panel. You will find that your database has been successfully registered.

Contact Us

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.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.