Read the database content

xiaoxiao2021-03-05  22

I want to find a use online.

CDAO *

Classes come from the data sheet or write data, but I didn't get any clues. Now you have completed this feature, from a Microsoft

ACCESS

file(

.Mdb

) Fast, dynamic, and simple creation

/

Write

/

Read data

. Our example is a dialog-based program.

Design our interface

In order to build our database interface, we should first we should be in the project.

STDAFX.H

Add to

Afxdao.h

statement of.

We will use two classes

CDaoDatabase

with

CDaorecordset

In the program, you need to use these two classes to construct them:

CDaodatabase Database; CDaorecordSet Recordset (& Database);

Create our database file

If we are dynamically created

ACCESS

Document, we don't need to use

CDaoDatabase :: open ()

,because

Create ()

The function will automatically open our connected database:

CString lpszfile = "c: //database.mdb"; Database.create (lpszfile);

Open an existing database file

Database.open (lpszfile);

Very good, now we have successfully connected to a database, then we can write data to the data table. Here first introduces the structure of some data tables, which is good for you to develop database development in the future.

A data table is a series of data sheets. You can use multiple data tables in a database file, each data sheet from row

(ROWS)

Column

(Column)

composition. Corresponding to the value of a row and column is called fields (

Field

). Below is an illustration of a standard data sheet:

Let us return to the subject. We first need to create a data sheet in our database file. What to do?

?

We create one by creating one

SQL

Execute a string to tell our database instance to execute it, for example

:

CString Sqlcmd = "Create Table MyTable (Name Varchar (20), Age Varchar (3));"; Database.execute (SQLCMD);

Above

SQL

Commands created a new name in our database file

MyTable

Data table. It has two columns, one is

Name

, One is

AGE

.

Name

Column data limit

20

Character,

AGE

Column data limit

3

Character. Very simple.

Note: You cannot create the same name data sheet in a file, unless you first use the correct

SQL

String call

EXECUTE ()

Function, delete this table.

Add data in a new data sheet

use

CDaorecordset

Example Open our database, when we create it for the first time, we will have one

CDaoDatabase

The pointer of the instance is now connected. In order to open our record and write data, we do the following code:

recordset.Open (AFX_DAO_USE_DEFAULT_TYPE, "SELECT * FROM MyTable", 0); database.Execute ( "INSERT INTO MyTable (Name);"); database.Execute ( "INSERT INTO MyTable (Age);"); recordset.AddNew ( ); RecordSet.setfieldValue ("name", "chris"); recordset.setfieldValue ("age", "13"); recordset.Update (); recordset.movenext (); recordset.addNew (); recordset.SetfieldValue "Name", "Joe"); RecordSet.SetfieldValue ("Age", "20"); RecordSet.Update (); Let's see the meaning of the above code:

1.

We use

SQL

command

Select * from myTable

Open the record instance. This means that the open function will get

MyTable

All data available in the data table, whether it is written or read.

2.

use

SQL

String call

EXECUTE ()

To join two new columns in the data sheet.

3.

transfer

AddNew ()

Allow us to join a new data

4.

Twice

SetFieldValue

The data is added to the two columns on the same line three, respectively.

5.

transfer

Update ()

Update the data of the data table.

6.

transfer

MoveNext ()

Move the record pointer to the next blank, repeat the method of adding the data above.

The above is a method of adding data to the data sheet. Here we describe how to read data from the data table. This part is also quite simple and easy to understand.

CDAORECORDSET :: getfieldvalue ()

The function has two parameters, one is the name of the column we want to get the data, which corresponds to the current location of the record pointer, we can move it freely by calling the following functions:

:: seek (); :: movefirst (); :: movenext (); :: moveprev () ;:: movelast ();

The second parameter is one

Colevariant

Example,

Colevariant

Be a base based on

COM

Object. Our field data will be stored in this object, but converting this storage object to we can identify

Cstring

The variable is what we need, the following code is to complete this work:

Colevariant olevar;

Confirm our

CDaorecordset

The instance has been opened, we call

While (! recordset.iseof ()); {olevar.changtype; RecordSet.GetfieldValue ("name", olevar); cstring strdata = (lpcstr) olevar.pbstrval; // code for Inserting Data Into a listbox , for exampleXT.MOVENEXT ();

Changeetype ()

Method

Colevariant

Instance conversion to

BSTR

(A hypothyroidistic string format). then we

PBSTRVALUE

Method conversion to

Cstring

We can use it in any way. These can be reused using the end of the identity record in the data table.

I believe that the above instructions are helpful to you.

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

New Post(0)