Directly read DAO, write Access files

zhaozj2021-02-08  219

Directly read DAO, write Access files

Author: Xu Jing Zhou

Download sample source code (http://www.copathway.com/itbookreview/view_paper.asp?paper_id=383)

Directly use DAO to create, read and write access files, always say, compare the article , you should be simple. In the examples below, we will use two methods: SQL and DAO classes to make them, doing this, I want to make everyone more convenient and flexible to use them to do what you want to do. In the sample program, you can specify the database name: demo.mdb, the internal table name is: demotable, write two fields: name and age, use and the previous reading and writing Excel similar operations, you can also need it according to yourself To dynamically change them. The sample program running interface is as follows:

Let's briefly look at its implementation steps:

1. First, make sure that it contains an AFXDAO.H header file, you can include it in the stdafx.h file, as follows:

#include // Join DAO Database Support

2. Declare the DAO library and its recordset, you can add the following code in your implementation file:

CDAODATABASE DB; // Database

CDAORECORDSET Recset (& DB); // Record Set

3. Next, let's take our creation and write operations first.

Void crwaccessdlg :: OnWriteAccess ()

{

/ / Get the path where the main program is located, there is spath

Cstring spath;

GetModuleFileName (null, spath.getbuffersetlength); max_path), MAX_PATH

Spath.releaseBuffer ();

Int npos;

Npos = spath.reversefind ('//');

Spath = spath.Left (npos);

/ / Create a data name by default: demo.mdb, internal table name: Demotable, two fields in the table: Name, age

CSTRING LPSZFILE = Spath "//demo.mdb";

CFILEFIND FFIND;

Bool bsuCcess;

BSUCCESS = ffind.findfile (lpszfile);

FFIND.CLOSE ();

/ / Do you have created a good demo.mdb file, not creating it

IF (! BSUCCESS)

{

db.create (lpszfile);

CString Sqlcmd = "CREATE TABLE Demotable (Name Varchar (20), Age Varchar (3));";

Db.execute (SQLCMD);

// Open the created data sheet

Recset.open (AFX_DAO_USE_DEFAULT_TYPE,

"SELECT * from demotable", 0);

/ / Add first record, use SQL statement

Db.execute ("INSERT INTO DMOTABLE (Name, AGE) VALUES ('Xu Jingze', 26)")

// Add a second record, use DAO

RecSet.addnew (); recset.setfieldValue ("Name", "Xu Zhihui");

Recset.setfieldValue ("Age", "21");

RECSET.UPDATE ();

// Add a third record, use DAO

RecSet.addnew ();

Recset.setfieldValue ("Name", "Guo Hui");

Recset.setfieldValue ("age", "27");

RECSET.UPDATE ();

// Close record set and library

RECSET.CLOSE ();

db.close ();

AfxMessageBox ("Access file is written in success!");

}

Else

AfxMessageBox ("Demo.mdb database has been created!");

}

4. Finally, let us implement its read operation.

Void CRWACCESSDLG :: OnReadAccess ()

{

Colevariant var; // field type

Var.ChangeType (VT_BSTR, NULL);

CString Strname, StRAGE, STRFILE

// Clear list box

m_accessList.resetContent ();

/ / Get the path where the main program is located, there is spath

Cstring spath;

GetModuleFileName (null, spath.getbuffersetlength); max_path), MAX_PATH

Spath.releaseBuffer ();

Int npos;

Npos = spath.reversefind ('//');

Spath = spath.Left (npos);

Strfile = spath "//demo.mdb";

DB.Open (STRFILE); // Open the created Demo database and Damotable table

Recset.open (AFX_DAO_USE_DEFAULT_TYPE, "SELECT * from Demotable", NULL);

While (! recsets.iseof ()) // Is there a table end?

{

Recset.GetfieldValue ("Name", var);

Strname = (lpcstr) Var.pbstrval;

Recset.GetfieldValue ("Age", VAR);

Strage = (lpcstr) Var.pbstrval;

M_accessList.Addstring (Strname "->" strage);

Recset.movenext ();

}

// Close record set and library

RECSET.CLOSE ();

db.close ();

}

The details of the specific implementation of the above part of the code can be downloaded after downloading the instance code, and the source code can be carefully viewed (in detail).

Contact information:

Email: jingzhou_xu@163.com

Future Studio (Future Studio)

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

New Post(0)