C ++ Builder dynamically set ODBC data source

zhaozj2021-02-11  165

This article takes Access as an example requires file odbcinst.h (BCB or VC's include directory) odbccp32.dll (winnt / system32 / directory) iMPlib.exe (Generate a library via DLL) Coff2omf.exe (convert the Coff library to OMF library)

First copy the above file to the project directory, execute the following command: IMPLIB.EXE ODBCCP32.LIB ODBCCP32.DLLCOFF2OMF ODBCCP32.LIB

The function of the following program is to dynamically add / modify a system data source called TEST to the current directory when the application starts.

New project, add #include USELIB ("ODBCCP32.LIB") in the project main file (including Winmain function); // lib is currently a current directory

The modified code is as follows; file: // ---------------------------------------- -----------------------------------

#include #include

#pragma hdrstopuseform ("main.cpp", form1); file: // Other units of the project declared ..... uSelib ("ODBCCP32.LIB"); file: // ---------- -------------------------------------------------- --------------- WinApi Winmain (Hinstance, Hinstance, LPSTR, INT) {Try {

Application-> Initialize (); ANSISTRING DATA, DATAPATH, DBPATH; UNSIGNED CHAR TEMP [255]; // Received transformation after connection string LPSTR CH; // string pointer is equivalent to char * dataPath = getCurrentDir (); // Get the current directory DATA = DataPath "// Test.mdb / b"; // Assume that the current database is Test.mdb dbpath = "dsn = test / bdbq =" Data "defaultdir =" DataPath "/ b / B"; File: // Configure the ODBC connection string, Test is the data source, the illegal character first use / b instead of CH = dbpath.c_str (); // Transforms Ansistring into string INT i = 0; while (* ch! = '/ 0') // Find and replace the illegal character, and load the character array {

IF (* ch == '/ b') * ch = '/ 0'; TEMP [i] = * ch ;CH ; i ;} if (:: sqlconfigdataroup (null, odbc_add_sys_dsn, (lpstr) "Microsoft Access Driver * .mdb) / 0 ", (lpstr) Temp) // Modify or add a data source, if successful, the program starts {Application-> Title =" Dynamic Set ODBC Data Source ";

Application-> Createform (__ classid (tform1), & form1); application-> run ();} else {showmessage ("database setting failed!"); Application-> terminate ();}} catch (exception & exception) {Application -> ShowException;} return 0;} file: // ------------------------------------------------------------------------------------------------------------------------------------------------------------------------ -----------------------------------------

Reference: C Builder App Development Daquan MSDN

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

New Post(0)