C ++ Builder: Create a BDE alias at runtime

zhaozj2021-02-17  51

C Builder: Create a BDE alias at runtime to connect the database through the BDE alias, which is really convenient, however, those that have not been determined, we can't create alias for it. Most of the time, when we give the program to others to truly put it in use, the path of the alias is not sure. This requires us to dynamically connect to the database during the program. Now, assume that there is a contact friends.db in the program running directory, we have to access the controls such as TQuery, TDataSource: I have learned for the learning program, if there is a mistake or chapter, please let me know

Add related controls on the form to declare the session handle HDBISES TMPSession in the header file;

To load the database at the time of the program, you can follow: void __fastcall tmainfrm :: formcreate (TOBJECT * Sender) {if (query1-> state == dsinactive) {dbiinit (null); // BDE initialization DBISTARTSession (null, tmpsssion, " "); // Open a temporary BDE session // set the alias path to the program where the program is located, pay attention to" PATH: "string ANSISTRING STRPATH =" PATH: ExtractFileDir (Application-> Exename); Dbiaddalias (NULL, "TmpMyfriends", "paradox", strpath.c_str (), false); // increase the alias to complete query1-> DatabaseName = "TmpMyFriends"; // can be used as usual. Query1-> Active = true;

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

When the program is closed, remember to release the resource of BDE, Void __fastcall tmainfrm :: form (Tobject * sender, tclosection & action) {if (query1-> Active) query1-> active = false; dbiclosesession (tmpsssion); // Close the current Session dbiexit (); // Remove BDE resources

}

转载请注明原文地址:https://www.9cbs.com/read-30207.html

New Post(0)