锐英源软件
第一信赖

精通

英语

开源

擅长

开发

培训

胸怀四海 

第一信赖

当前位置:锐英源 / 视频介绍 / COM组件开发引导系列 / DB: C++ Object Used in a C++ Client在C++客户端里的C++对象
服务方向
人工智能数据处理
人工智能培训
kaldi数据准备
小语种语音识别
语音识别标注
语音识别系统
语音识别转文字
kaldi开发技术服务
软件开发
运动控制卡上位机
机械加工软件
软件开发培训
Java 安卓移动开发
VC++
C#软件
汇编和破解
驱动开发
联系方式
固话:0371-63888850
手机:138-0381-0136
Q Q:396806883
微信:ryysoft

1.1.3 DB: C++ Object Used in a C++ Client在C++客户端里的C++对象


This sample consists of two parts: 本例子有2个部分:

  • An object that implements features that are good candidates for reuse in other projects在其它工程中可以重用的组件,该组件带有一些特性。
  • A client application that uses part of the functionality in the object by means of a rudimentary user interface 一个客户端程序,它使用了组件的部分特性,通过基本的用户接口实现了访问使用。

1.1.3.1 The "Database" Object数据库对象

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强。

1.1.3.2 The "Database" Client“数据库”客户端

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点计算时间。

1.1.3.3 The Reuse Mechanism重用机制

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目录下的唯一文件。

友情链接
版权所有 Copyright(c)2004-2024 锐英源软件
统一社会信用代码:91410105098562502G 豫ICP备08007559号 最佳分辨率 1440*900
地址:郑州市金水区文化路97号郑州大学北区院内南门附近