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 and 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;
//---------------------------------------------------------------------------
#include <vcl.h>
# Include <odbcinst.h>
#pragma hdrstop
Useform ("Main.cpp", Form1),
//other unit of project declaration ....
Uselib ("Odbccp32.lib");
//---------------------------------------------------------------------------
WINAPI WinMain (HINSTANCE, HINSTANCE, LPSTR, int)
{
Try
{
application->initialize ();
Ansistring Data,datapath,dbpath;
unsigned char temp[255];//receives the converted connection string
LPStr ch;//string pointer equals char *
Datapath=getcurren Tdir ()//Gets the current directory
data=datapath+ "\\test.mdb\b";//assumes the current database is Test.mdb
dbpath= "dsn=test\bdbq=" +data+ "de Faultdir= "+datapath+" \b\b ";
//construct ODBC connection string, test is data source name, illegal character Fuxian \b instead of
Ch=dbpath.c_str ();//Convert ansistring to String
in T i=0;
while (*ch!= ')//finds and replaces illegal characters, and loads the character array
{
if (*ch== ' \b ')
*ch= ' ";
Temp[i]=*ch;
ch++;
i++;
}
if (:: SQLConfigDataSource (Null,odbc_add_sys_dsn, (LPSTR) Microsoft Access Driver (*.mdb) ", (LPSTR) temp)//modify or add a data source, if successful, the program starts
{
Application->title =" Dynamically set ODBC data source ";
Application->createform (__classid (TFORM1), &form1);
Application->run ();
Else
{
ShowMessage (database setting failed!) ");
Application->terminate ();
}
catch (Exception &exception)
{
Application->showexception (&exception);
}
return 0;
}