Set up ODBC data source with Visual C ++ program

zhaozj2021-02-11  235

Set up ODBC data source with Visual C program

Suzhou Power Supply Bureau Information Center Tang Yun

---- ODBC (OPEN DATABASE CONECTIVITY), an open database interconnection, an important part of the Windows open structure has been familiar with a lot of Windows programmers, and ODBC's work relies on the driver provided by the database manufacturer, using ODBC When the API, the Windows ODBC manager passes the request of the database to the correct driver. The driver uses the SQL statement to indicate DBMS to complete database access, so ODBC's existence provides our development application database programs to provide very powerful Ability and flexibility.

---- In order to make ODBC work with the database, you must register the database to the ODBC driver manager, which can be done by defining a DSN or data source name. Usually, we can only manually open the system control panel, run the ODBC Data Source Manager, manually configure the data source, but this work is too complicated to the user, we must consider completing these configurations for users.

---- So many programmers want to have an excellent installer to automatically set ODBC data sources when publishing their own database software, although INSTALLSHIELD can now help us achieve such functions , But after all, it is lacking, the programmer can't fully control it. In fact, we can write some programs to implement such functions. There are several ways to implement, one is to modify the Windows registry, programmers can use The Windows API function changes the key value in the ODBC.ini under HKEY_LOCAL_MACHINE / SOFTWARE / ODBC, which is more cumbersome. I now recommend a way to use ODBC API in the program, programmers can invoke these API functions to programs written in Visual C at any time.

---- Here I use MFC to write a program to demonstrate how to implement this feature:

---- First, open Visual C , select New on the File menu, then select the MFC AppWizard (EXE) class, Project Name We are set to try, press the OK button, and select Dialog Based in the next step 1. Because you don't have to use later options, you can press the finish key at this time, and the resulting system will generate a new project. After completing the above work, in the Workspace window on the left, select ResourceView, open the Dialog resource in Try Resources, select and open the IDD_TRY_DIALOG dialog window, click on the button icon in the Controls menu window, return to the IDD_TRY_DIALOG dialog window and click on this window, A button named Button1 will be generated. Check this button to right-click, select the Properties option on the pop-up menu, change the button1 value of the CAPTION item to Setup ODBC in the dialog box appear, close this dialog, then select This button is right-click, select ClassWizard, in the interpretation window, Object IDS selection IDC_Button1, Messages Double-click BN_Clicked, and pop-up the Add Member Function dialogue, Member Function Name is OnButton1, press the OK button. Double-click Member functions options onButton1 ON_IDC_BUTTION1: BN_CLICKED, in the event of a void CTryDlg :: OnButton1 () function with the following statement replaces the ODBC API function // TODO: Add your control notification handler code here this comment statement: 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 above statement based on your different settings, SQLConfigDataSource generally has the following parameters: ODBC_ADD_DSN: Add a new user data source, ODBC_CONFIG_DSN: Modify an existing user data source, ODBC_Remove_DSN: Delete An 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 provincial deficient data source description section. It should be noted that when we use the SqlConfigDataSource ODBC API function, we must declare the odbcinst.h header file containing the system, so we select the FileView in the Workspace window to open Header Files in Try.h, add #include "odbcinst.h". If this header is not added, the Undeclared Identifier error is displayed when the system is compiled. After the above steps are completed, if we immediately compile and link this project, it will find the following error:

Trydlg.obj: error lnk2001: unresolvedexternal symbol _sqlconfigDataSource @ 16

Debug / try.exe: Fatal Error LNK1120:

1 Unresolved Externals

---- Some people may give up because they can't find mistakes, in fact, this is because when we use SqlConfigDataSource this API function, you must use odbccp32.dll, which is the 32-bit ODBC installation and management of Microsoft, if It is 16 bits to be used to 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, select the Add to Project submenu, In the selection of the Files item, open the / vc / lib / directory in the VC installation directory, the file type selection library files (.lib), select the ODbccp32.lib, press the OK button, then recompile, run this program, The dialog window will pop up, press the Setup ODBC button, then you can view the run results by the ODBC Data Source Manager or registry of the control panel, you will find that your database has been successfully registered.

转载请注明原文地址:https://www.9cbs.com/read-4537.html

New Post(0)