This article takes access as an example
Need file Odbcinst.h (BCB or VC's include directory) Odbccp32.dll (winnt/system32/directory) Implib.exe (Generate Lib library via DLL) Coff2omf.exe (Convert COFF library to OMF Library)
First copy the above file to the engineering directory,
Execute the following command:
Implib.exe Odbccp32.lib Odbccp32.dll
COFF2OMF Odbccp32.lib
The function of the following program is to dynamically add/modify a system data source that points to the current directory, named Test, when the application is started.
Create a new project to add in the project Master file (with the WinMain function)
#include <odbcinst.h>
Uselib ("Odbccp32.lib");//lib in the current directory of the project
The modified code is as follows;
file://---------------------------------------------------------------------------
#include <vcl.h>
#include <odbcinst.h>
#pragma hdrstop
Useform ("Main.cpp", Form1);
Other units of the FILE://project declare .....
Uselib ("Odbccp32.lib");
file://---------------------------------------------------------------------------
WINAPI WinMain (hinstance, HINSTANCE, LPSTR, int)
{
Try
{
Application->initialize ();
Ansistring Data,datapath,dbpath;
unsigned char temp[255];//receive transformed connection string
LPSTR ch;//string pointer equals char *
Datapath=getcurrentdir ()//Get current directory
data=datapath+ "//test.mdb/b";//assuming the current database is Test.mdb
Dbpath= "dsn=test/bdbq=" +data+ "defaultdir=" +datapath+ "/b/b";
file://constructs the ODBC connection string, test is the data source name, the illegal character Fuxian with/b instead
ch=dbpath.c_str ()//convert ansistring to String
int i=0;
while (*ch! = '/0 ')//find and replace illegal characters, and mount a character array
{
If (*ch== '/b ')
*ch= '/0 ';
Temp[i]=*ch;
ch++;
i++;
}
if (:: SQLConfigDataSource (Null,odbc_add_sys_dsn, (LPSTR) "Microsoft Access Driver (*.mdb)/0", (LPSTR) temp)//modify or add a data source , if successful, the program starts normally
{
Application->title = "Dynamically set ODBC data source";
Application->createform (__classid (TFORM1), &form1);
Application->run ();
}
Else
{
ShowMessage ("Database settings failed.) ");
Application->terminate ();
}
}
catch (Exception &exception)
{
Application->showexception (&exception);
}
return 0;
}
file://---------------------------------------------------------------------------
Reference: C + + Builder application Development Encyclopedia MSDN