Implement ODBC Data Source settings with VC ++

zhaozj2021-02-16  61

ODBC (Open Database CONECTIVITY), an open database interconnection, as 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, when using the ODBC API, 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 and flexible Sex. 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.

Therefore, many programmers want to have an excellent installer to automatically set ODBC data sources when publishing their own database software, although now INSTALLSHIELD is now able to help us implement such functions, but lacks after all Flexible, programmers 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, and the programmer can use the Windows API function to increase Change the key value in the odbc.ini under HKEY_LOCAL_MACHINESOFTWAREODBC, 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.

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

First, open Visual C , choose New on the File menu, then select the MFC AppWizard (EXE) class, Project Name We are trying to try, press the OK button, and select Dialog Based in the next Step 1 screen, because you don't have to use it The following options can be pressed, 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 = trydb0dbq = D: Database

Try.mdb0Defaultdir = d: Database00 ");

'

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 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 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, 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 must With 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, choose the Add to Project submenu, choose Files Item, open the vclib directory in the VC installation directory, the file type selection library files (.lib), select the ODbccp32.lib, then press the OK button, then re-compiling, run this program, pop up the dialup, press 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.

All the above code is compiled in Win98, VC6.0, you can flexibly applying these methods, making your software easier to install maintenance and use.

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

New Post(0)