C + + Builder dynamically set ODBC data source

Source: Internet
Author: User

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


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.