Use Access Database in C ++

Source: Internet
Author: User
Use Access Database in C ++

Access database has been widely used in management. In the long-term use, many data files are stored in Access database files. When C ++ builder is used for programming, it is often necessary to use an Access database to transmit data.

In C ++ builder, access database can be implemented either during the design period or during the runtime through programming. The following is a simple example to illustrate how to use the ACCESS database in C ++ builder.

 
Use Access database during design

Create a new project, save the unit file unit1.cpp, and save the project file project1.bpr. Assume that there is an Access database file student. MDB in the path where the project file is saved. Create an alias for the database. Click the Tools menu item of C ++ builder and click Database Desktop in the drop-down menu to enter the database system that comes with C ++ builder. Select Alias Manager in the tools item of Database Desktop to create an alias for the access database. To create an alias, click "new". In the database alias column of the pop-up dialog box, create an alias for the database, for example, "mydatabase". In the drive type column, select msaccess; fill in the path and file name of the Access database file in the database name column. Fill in the path and file name for other projects as needed, and click "OK" to bring up a dialog box for saving alias settings, select "yes" to create the database alias.

Place a tdatabase control database1 in form form1 and set the following attributes: select the created alias "mydatabase" for the aliasname attribute, and enter an application for the databasename attribute.ProgramA dedicated alias, such as "app_alias" (no need to create); select true for the connected attribute. A database logon dialog box is displayed. If no user name or password is set, click "OK ", if you have a user name and password when creating an alias, enter the correct information and click "OK" to connect to the database.

Add a tTable control Table1 in form form1 and fill in a special alias for the application in the databasename attribute. In this example, "app_alias" is used, and select a required data table name in the tablename attribute, set the active attribute to true. Add a tdatasource control performance1 to form1 and set its dataset attribute to table1. Add the TDBGrid controls dbgrid1 and tdbnavigator controls dbnavigator1, set their datasource attributes to performance1, so that you can see the content in the ACCESS database, you can also use the dbnavigator1 control to perform some simple operations on the database. You can program Table1 to perform more operations on the database. In the design period, the ACCESS database is simple and quick, but the flexibility is poor.

Use Access database at runtime

When using the ACCESS database at runtime, in the form form1, in addition to the above controls, you also need to add a BDE Session object, that is, the tsession control session1. Set the autosessionname attribute to true, and add the following to the oncreate event function of form1:Code:

Ansistring app_path, alias_1, name_1;

App_path = extractfilepath (paramstr (0 ));

// Obtain the Application Path

Alias_1 = "mydatabase2 ";

// Access Database alias

Name_1 = "database name =" + app_path + "123.mdb ";

If (! Session1-> isalias (alias_1 ))

// The database alias does not exist.

{

Tstringlistdataname = new tstringlist ();

Dataname-> Add (name_1 );

Session1-> addalias (alias_1, "msaccess", dataname); // create an 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, do not forget to enter BDE administrator to change the dll32 in msaccess driver configuration from iddao32.dll to idda1_2.dll and save the configuration.

To make the program run more smoothly, you can add the following code to the onlogin event function of database1:

Loginparams-> values ["password"] = "";

Loginparams-> values ["username"] = "";

In this way, the database Login Dialog Box will not pop up when running the program.

The above program is in the Windows 95 operating system, and the C ++ builder 3 has passed the trial run.

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.