Access database has been widely used in management, in the long-term use of a lot of data stored in the Access database files, in C + + Builder programming, often also need to use Access database, to achieve data transfer.
Using an Access database in C + + Builder can be done either at design time or programmatically at run time. The following is a simple example of how to use an Access database in C + + Builder.
Using an Access database during design time
First create a new project, save the unit file for Unit1.cpp, save the project file for PROJECT1.BPR. Suppose you have an Access database file Student.mdb under the path to save the project file, and you need to create an alias for the database. Click on C + + Builder tools selected items, in the Drop-down menu click Database Desktop entered the C + + Builder database system. Then select Alias Manager in the Tools item in database desktop to create an alias for the Access database. The alias is created by first clicking on the "New" button, and then an alias for the database, such as "MyDatabase", in the name of the pop-up dialog box, and a msaccess in the drive type bar; The name bar fills in the path and file name of the Access database file, the remaining items can be filled out as needed, and then the "OK" button pops up a dialog box that saves the alias settings and selects "Yes" so that the database alias is created.
Place a Tdatabase control Database1 in the form Form1 and set the following properties: The AliasName property selects the alias "MyDatabase" that has been created, and the DatabaseName attribute fills in an application-specific alias, such as "App_ Alias (no need to create); Connected property to select True, this will pop up a database Login dialog box, if not set username and password, then click "OK", if you create an alias with a username and password, correctly fill in and then click "OK" can be connected to the database.
Add a Ttable control Table1 in the form Form1 and fill out the application's private alias in the DatabaseName property, in this case, "App_alias", select a desired data table name in the TableName property, and set the active property to true. Add a Tdatasource control DataSource1 to the form Form1 and set its DataSet property to Table1. Add a Tdbgrid control DBGrid1 and Tdbnavigator control DBNavigator1 and set their datasource properties to DataSource1 so that you can see the contents of the Access database. And through the control DBNavigator1 can do some simple operations on the database. If you want to do more operations on the database can be achieved by TABLE1 programming, no longer repeat here. Using an Access database at design time is simple, fast, but less flexible.
Using an Access database at run time
Using an Access database at run time, in addition to the above controls in the form Form1, you need to add a BDE session object, that is, the TSession control Session1. Set the Autosessionname property to True, and then add the following code to the Form1 OnCreate event function:
Ansistring app_path,alias_1,name_1;
App_path=extractfilepath (paramstr (0));
Get the path to the application
Alias_1= "Mydatabase2";
Access database Alias
name_1= "DATABASE name=" +app_path+ "123.mdb";
if (! Session1->isalias (Alias_1))
Database alias does not exist
{
Tstringlist *dataname=new tstringlist ();
Dataname->add (name_1);
Session1->addalias (Alias_1, "MSAccess", dataname);//Create Alias
Delete Dataname;
Session1->close ();
}
Database1->aliasname=alias_1;
Database1->databasename= "App_alias";
Table1->databasename=database1->databasename;
Table1->tablename= "Xxys";
Table1->active=true;
Note: Before running the above program, don't forget to enter the BDE administrator to change the DLL32 from IDDAO32.DLL msaccess driver configuration to IDDA3532.DLL and save this configuration.
To make the program run more smoothly, add the following code to the Database1 onlogin event function:
loginparams->values["PASSWORD"]= "";
loginparams->values["USERNAME"]= "";
This will not eject the Database logon dialog box when you run the program.