精通
英语
和
开源
,
擅长
开发
与
培训
,
胸怀四海
第一信赖
This sample consists of two parts: 本例子有2个部分:
The DB sample application implements a simple, database-like object that manages all its data in memory. Each "database" contains an array of pointers to the tables inside the database. The tables are arrays of strings, and each array element represents one row in the table. A parallel string array to the pointer array contains the "names" of the tables.DB例子实现了一个简单的象数据库的对象,它在内存中管理所有数据。每个“数据库”包含一个指针数组,里面指针指向数据库里的表。表是字符串数组,每个数组元素代表了一个表里的一行。还有一个和指针数组对应的字符串数组包含了表的“名称“。
Queries are extremely simple: just indicate a table number (a zero-based index to the array of table pointers) and the row number (a zero-based index into the nth string array). Thus, the Read function looks like this:查询也简单:只需要指定表数字(基于0的在表指针数组范围内的索引)和行数字(基于0的索引,在字符串数组里指示第几个的索引)。
HRESULT Read(short nTable, short nRow, LPTSTR lpszData);
The application passes in a preallocated buffer of sufficient size, and the object copies the string data to this buffer.应用程序传递一个预先分配好的足够大的内存区,对象拷贝字符串数据到这个内存区里。
The Write function looks very similar:Write函数如下:
HRESULT Write(short nTable, short nRow, LPCTSTR lpszData);
Additional functions let you create tables (giving each table a name), remove tables, and obtain information about the database:额外其它的函数有:创建表(给定每个表一个名称),删除表和获取数据库的信息。
typedef long HRESULT;
class CDB { // Interfaces public: // Interface for data access HRESULT Read(short nTable, short nRow, LPTSTR lpszData); HRESULT Write(short nTable, short nRow, LPCTSTR lpszData); // Interface for database management HRESULT Create(short &nTable, LPCTSTR lpszName); HRESULT Delete(short nTable); // Interface for database information HRESULT GetNumTables(short &nNumTables); HRESULT GetTableName(short nTable, LPTSTR lpszName); HRESULT GetNumRows(short nTable, short &nRows); // Implementation private: CPtrArray m_arrTables;// Array of pointers to CStringArray (the "database") CStringArray m_arrNames; // Array of table names public: ~CDB(); };
All functions return the same kind of error/success code, which, in general, is a good design for a class, because it facilitates error reporting even on simple functions such as CDB::GetNumTables, where one is tempted to simply return the "short" value. If there was something wrong with the database, the function can return something more intelligent than "0" or "-1".所有的函数返回同样类型的失败/成功编码,这个编码通常来说是类的良好设计风格,因为它促成了错误报告,甚至对于简单的函数比如CDB::GetNumTables,这个函数里尝试只是返回一个“short“值。如果遇到了错误,函数能够返回的信息更灵活,这比只返回0或-1强。
The client is an MFC/AppWizard-generated Windows-based application. Each document creates a new CDB object in its OnNewDocument function and stores a pointer to the object in CDB::m_pDB.客户端是一个MFC向导生成的窗体程序。每个文档创建一个新的CDB对象,创建位置是在它的OnNewDocument函数里,且在CDB::m_pDB里存储了一个对象的指针。
The document class implements four menu items: 文档类实现了四个菜单项。
Create创建
Write写入
Read读取
Read multiple 读取多个
Create adds a new table called "Testing" to the database associated with the document and saves the number of the last created table in a member variable (m_nTable) of CDBDoc.创建功能添加了一个新表到和文档相关的数据库里,表名叫”Testing”,且表里保存了在成员变量(m_nTable)最新创建表用的数字。
Write writes a generated string into row 0 of the last created table.写功能向第0行里面写入一个字符串。
Read reads whatever is stored in row 0 of the last created table and stores it in CDBDoc::m_csData, which the view class CDBView displays in the client area. 读取功能读取了最新创建表的第0行,保存到CDBDoc::m_csData变量里,而CDBView类显示了这个变量。
Read multiple performs as many reads as indicated in the DB.INI file "ReadMultiple" section, in the entry "NumCalls," and measures the time this took using the system-provided tick count.读取多个功能读取了DB.INI文件里”ReadMultiple”节里的“NumCalls”入口指示对应的多个结果。并且用系统提供的tick点计算时间。
The DB sample uses standard C++ source code reuse, for which the client needs the complete source code for the object and compiles it into the project. The project uses three directories: \CLIENT, \INTERFACE, and \OBJECT. The \INTERFACE directory contains the header file DBSRV.H, which declares the object (see above). This file is used by both the client and the object. The client directly includes DBSRV.CPP, which is the only file in the \OBJECT directory.使用了C++重用机制,每个客户端需要完整的组件源代码,且编译到自己工程里。工程使用了三个目录:\CLIENT,\INTERFACE和\OBJECT。\INTERFACE目录包含头文件DBSRV.h,这个头文件声明了对象。这个文件在客户端和对象里都使用了。客户端直接包含了DBSRV.CPP,这是在\OBJECT目录下的唯一文件。