C + + Builder create BDE aliases at run time

Source: Internet
Author: User
Tags aliases

It is really convenient to connect to the database by BDE aliases, but we cannot create aliases for those databases that are not identified.

And most of the time, when we give the program to someone else to actually put it into use, the path to the alias is also uncertain. This requires that we connect to the database dynamically while the program is running.

Now, let's say that there is a directory friends.db in the program's running directories, and we're going to call through Tquery, Tdatasource, and so on:

I learn programming soon, if there is a mistake or missing, please enlighten me

Add related controls on a form

To declare a session handle in a header file

Hdbises tmpsession;

To enter the database in the program startup fashion, you can:

void __fastcall tmainfrm::formcreate (tobject *sender)
{
if (query1->state==dsinactive)
{

Dbiinit (null);//bde initialization
Dbistartsession (Null,tmpsession, "");/open a temporary BDE session
To set the alias path as the program's directory, be careful to add the "path:" String ahead.
Ansistring strpath= "Path:" +extractfiledir (Application->exename);
Dbiaddalias (NULL, "Tmpmyfriends",
"Paradox", Strpath.c_str (), FALSE)//Add alias complete
Query1->databasename= "tmpmyfriends";//The next can be used as usual.
query1->active=true;
}

Query1->sql->clear ();
Query1->sql->add ("SELECT * from friends.db");
Query1->open ();
}

When the program shuts down, remember to release the resource that BDE occupies

void __fastcall Tmainfrm::formclose (tobject *sender, tcloseaction &action)
{
if (query1->active)
query1->active=false;
Dbiclosesession (tmpsession);//Close Current session
Dbiexit ()//release BDE resources
}

The dbistartsession and Dbiaddalias functions can be explained.

The Dbistartsession function syntax is as follows:

Dbiresult DBIFN dbistartsession ([Pszname], phses, [Pnetdir]);

Where the Pszname parameter is a PCHAR type pointer that is named for the most recently opened session, can be null and let the BDE be arbitrarily specified, and the phses parameter is used to specify the handle of the session you are looking for. Pnetdir is a pointer to the network path, and I have not used it specifically.

The function call succeeds, it returns Dbierr_none.

The Dbiaddalias function syntax is as follows:

Dbiresult Dbiaddalias ([hcfg], Pszaliasname, Pszdrivertype, Pszparams, bpersistent);

Where hcfg specifies the configuration file to use, NULL sets the current session to the configuration file, Pszdrivertype indicates the database-driven type, and Null is represented by the standard type. Bpersitent is a bool value, specifies whether to keep the alias in the configuration file, if

False the alias to be added is only used in the current session. function returns Dbierr_none if the call succeeds.

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.