C + + Builder dynamically set ODBC data source

Source: Internet
Author: User
Tags odbc odbc connection

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;
}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.