Use DAO directly to create, read and write access files, in general, in contrast to the previous article, "Read and write Excel files directly through ODBC," to be simpler. In the following example, we'll use two methods: SQL and DAO class functions to mix them up, so in order to do so, I think we can make it easier for everyone to use them to accomplish what you want to do. The default designation in the sample program is to create the database named: Demo.mdb, the internal table name is: demotable, write two fields: name and age, with the previous read and write Excel similar operations, you can also dynamically change them according to their own needs. The sample program runs the interface as follows:
Let's take a quick look at its implementation steps:
1. First, be sure to include the afxdao.h header file, which you can include in the StdAfx.h file, as follows:
#include <afxdao.h> //加入DAO数据库支持
2. Declare the DAO library and its recordset variables, and add the following code to your implementation file:CDaoDatabase db; //数据库
CDaoRecordset RecSet(&db); //记录集
3. Next, let's implement its creation and write operationsvoid Crwaccessdlg::onwriteaccess ()
{
Gets the path of the main program, which exists in spath
CString spath;
GetModuleFileName (Null,spath.getbuffersetlength (max_path+1), MAX_PATH);
Spath.releasebuffer ();
int NPOs;
Npos=spath.reversefind (' \ \ ');
Spath=spath.left (NPOs);
Default Create Data name: Demo.mdb, Internal table name: demotable, Table has two fields: name, age
CString lpszfile = spath + "\\Demo.mdb";
CFileFind Ffind;
BOOL bsuccess;
Bsuccess=ffind.findfile (lpszfile);
Ffind.close ();
Whether there is already a created Demo.mdb file, no create it
if (!bsuccess)
{
Db. Create (lpszfile);
CString SQLCMD = "CREATE TABLE demotable (Name VARCHAR (), Age VARCHAR (3));";
Db. Execute (SQLCMD);
Open a data table that you have created
Recset.open (Afx_dao_use_default_type,
"SELECT * from Demotable", 0);
Add the first record, using the SQL statement
Db. Execute ("INSERT into Demotable (name,age) VALUES (' Xu Jingzhou ', 26)");
Add a second record, with DAO culvert number
Recset.addnew ();
Recset.setfieldvalue ("Name", "Xuzhihui");
Recset.setfieldvalue ("Age", "21");
Recset.update ();
Add a third record, with DAO culvert number
Recset.addnew ();
Recset.setfieldvalue ("Name", "Guo Hui");
Recset.setfieldvalue ("Age", "27");
Recset.update ();
Close Recordsets and libraries
Recset.close ();
Db. Close ();
AfxMessageBox ("Access file Write succeeded!") ");
}
Else
AfxMessageBox ("Demo.mdb database has been created!") ");
}
4. Finally, let's implement its read operation. void Crwaccessdlg::onreadaccess ()
{
COleVariant var; field Type
var. ChangeType (VT_BSTR, NULL);
CString Strname,strage,strfile;
//Empty list box
M_accesslist.resetcontent ();
Gets the path to the main program, which exists in spath
CString spath;
GetModuleFileName (Null,spath.getbuffersetlength (max_path+1), MAX_PATH);
Spath.releasebuffer ();
int NPOs;
Npos=spath.reversefind (' \ \ ');
Spath=spath.left (NPOs);
strfile = spath + "\\demo.mdb";
Db. Open (strfile); Open the demo database that you have created and the Damotable table
Recset.open (afx_dao_use_default_type, select * from demotable, NULL);
while (! Recset.iseof ())//There is no end to the table
{
Recset.getfieldvalue ("Name", Var);
StrName = (LPCSTR) var.pbstrval;
Recset.getfieldvalue ("Age", Var);
Strage = (LPCSTR) var.pbstrval;
M_accesslist.addstring (strName + "-->" +strage);
Recset.movenext ();
}
//close Recordset and library
Recset.close ()
Db. Close ();
}
The details of the implementation of the above part of the code, you can download the instance code, carefully check the source can be (with detailed comments).