Most mfc developers are familiar with the open database connectivity (odbc) database class - they are back in three and a half ago there. In mfc 4.2 in these classes have some important improvements.
As c + + developer, you may not be familiar with the dao, because so far it only be used for microsoft access and visual basic? Programming System. If you are familiar with the dao, we must understand: dao in mfc in the realization of quite different, but inclusive.
Learn mfc odbc class developers need to know: Although mfc dao more analog odbc functional, but can not replace them dao class.
For those not very familiar with the mfc of people, I would like to remind: mfc to have a class for a variety of database design principles.
mfc like a thin wrapping paper as packaging windows? operating system api, provided you want the c + + performance, while providing you need to abstraction. When the foundation of practical significance api, to add value. But most of the time trying to maintain a non-mfc hit man.
As mfc api package as more complex, the database class is also more complex packaging techniques. Because mfc in intel, unix, and shift between alpha value, so solutions are database applications can shift value.
We microsoft access, visual basic recordset using the same model, therefore, have used the product developers who need to learn a new paradigm. In addition, two class sets of the same architecture, using one of a class set of developers can easily switch to another class of set and use it.
mfc Database class
Since version 1.5, after, mfc in the database already contains the odbc classes. visual c + + 2.0 version provides the same type with 32-bit version. These classes based on an industry accepted standards, and has been widely used, and its portability has been other odbc database development options of all ages. This portability is the ability to use many odbc data source with applications created these classes for use with. Recent performance improvements make odbc database class to become an attractive option.
In most cases, mfc 4.0 in the dao database type allows you to directly access the desktop data sources without having to use odbc. dao database classes have the same type of ability to open multiple databases, the ability to use multiple data sources and data definition language ability, making it an important development options.
Now you are ready to raise the issue, they should use what the database type set. If you provide little information about their projects, it is difficult to determine the answer to this question. However, the first thing to consider is what you use data source. If you normally use the desktop data, we encourage you to consider mfc dao database class, because you will find they are the most effective and powerful. If you mainly use the odbc (server-based) data, use the class-based odbc progress of the project will make you more productive.
Other considerations include network types, scalability requirements, and the speed of the most important factor. It is best that works best with your database cluster to maintain a prototype application. Can some benchmark tests to determine the best performance of your choice.
The real decision in your
You can use to create database applications a great set of options. It actually is a complete range of options, one end is a desktop database application, the other end is a strict client / server database applications. Only two seemingly obvious choice might be: the data in microsoft access 97 mdb database using mfc dao class, and in the microsoft sql server? 6.5 using mfc odbc database class. These two combinations are mainly used in conjunction with each other, and are very effective. But you may have an understanding of these choices, otherwise how?
Briefly, the following is your decision-making process which:
◆ Data Source needs to determine
How much you need a database? Time there will be many people who need to access data? Sometimes, most of the remaining steps are depending on your choice of data source.
◆ determine the interface requirements
If you need to interface a large number of user input (such as the user can design their own inquiries), then the adjustment and distribution of the database (one or more) must be careful when. For example, if data filled with little change in the list box, and have chosen a server-based data source, it will usually not change the data stored in the local rather than the server, is a meaningful approach.
◆ determine the connectivity needs of
Network protocol currently in use there are many, each of agreements on data transmitted through the network have different effects. Discussion on the Internet is not a matter within the white paper copies, but you need to know how to optimize their database applications, to avoid the pitfalls encountered in the data network.
◆ Select the appropriate tool (one or more)
microsoft for database developers with a variety of optional tools (microsoft access, visual basic, visual basic enterprise, visual c + + / mfc and sql server), in some cases, their functions overlap slightly. This white paper aims to discuss the visual c + + / mfc option. But if you have not considered other options, they should also understand a little.
◆ maintain the prototype before the implementation
In this article you will see this statement several times, because it is very important. You may have done a very good choice, but unless your solution really played a role, or work is not complete!
In addition, with regard to this article, we assume that you want to know is the mfc's database class. We may wish to check the number as much as possible options so that you acknowledge that you want to own the guidance of the first prototype used.
Crecordview is a mfc class structure - a form of display data. Because crecordview is based cformview, so it has all the basic genetic function of the class. In essence, the form view in the window client area extended dialog box template. This makes the added control and display field data is extremely easy.
When using appwizard and classwizard create database applications based on odbc, the recordset column will automatically bind to (statically) member variables, these variables can then add to the dialog template.
A cdbexception object represents a type caused by a database exception error conditions. Class contains two public data members, can be used to determine the two members of the leading causes exception error, or show abnormal errors described in the text message. cdbexception object class member function by the database structure and thrown.
cfieldexchange class supports the use of the records from the database class field exchange (rfx) routine. If you are writing a custom data types of data exchange routines, the use of such; Otherwise, you will not directly use the class. rfx in your recordset field data members of object and data source on the current record to exchange data between the corresponding fields. rfx both directions in the management of the exchange, that exchange from the data source to data source with the exchange.
The dao of the native format, contains 21 objects and 20 sets. Moreover, dao not only provide a single object, such as tables and fields, it also provides a collection of objects belong. Object makes this clear hierarchy can easily apply the principles of object-oriented database development.
dao existence for some time. dao 1.0 version in microsoft access 1.0 version, it only provides the structure to the table and the query interface, and has a limited number of properties on behalf of the table, dynamic set, a snapshot of the object. visual basic 3.0 version of the data access objects 1.0 adds tabledef, querydef and field object to provide a programmable structure.
microsoft access 2.0 version of the dao 2.0 version with ole automation prototype, as well as almost all the microsoft jet features access to all programming. It has a reliable set of all objects and attributes in the object model.
dao 2.5 version contains odbc desktop database drivers (odbc desktop database driver), these drivers are for the 16-bit platform with odbc desktop database drivers 2.0 version created for use with. The 16-bit version is for use in visual basic 4.0 version of the 16-bit version installed.
dao 3.0 version comes with the microsoft access for windows 95, visual basic 4.0 version (32), microsoft excel 7.0 version and visual c + + 4.0 version. dao been enhanced to support single interface to any compatible host.
dao 3.5 version comes with the microsoft access for windows 97, by and with the new odbcdirect com object. mfc dao database object class does not include these classes.
Most importantly, dao interface is based on ole com, it dao well placed to adapt to evolving technologies and operating systems.
Here is the dao hierarchy chart. In the top, you will see dbengine object, which contains all other objects. This is not the only collection of objects, because you can only have one engine. But it can have multiple workspaces (workspace), database (database) and so on, which is the object of the rest of their collection has its own reasons.
In the work area, you can have multiple databases, a basic form (. Mdb) or an attached / linked tables. In each database will have one or more tables, queries, records set, of which each table, query, recordset only contains field and (or) Index, as well as other types of objects.
In addition, the connection with the work area is the user objects and group objects, they form the dao of the security model.
Separate but connected with the engine object is the object errors.
errors in the collection of additional means object is different from other dao collection. The most detailed collection of errors on the end of an error on the most conventional start.
mfc and dao
Mfc now talking about how dao. Because we are not wrapped each dao object, it will actually mfc dao flat hierarchy. We provide you with 8 objects, rather than 21 objects.
We package all of the dao functions, in addition to security target groups - users objects, groups objects and new odbcdirect object. It is our intention into it. For example, we feel that the security object in view, they create the class will not be around to use dao to add any value, so it allows you to directly call the dao, to deal with those objects. This is also consistent with the mfc principles: It should be in the position to add value in terms of meaningful places create a class. But we will in the mfc technical note 54 to provide information on how to achieve security model guidance.
In addition, we also manage dao object to append to the collection requirements. In the dao, you create the object, then it is appended to the collection. In addition to an exceptional situation, this add is done automatically. For such exceptional circumstances, can be used as a separate step to set the developer can additional objects, it is used.
It may use the odbc database class for dynamic binding, but to achieve this functionality built-in feature is not mfc class. It was placed dao database class, you can quite easily dynamic binding. Now our second demo will be more talk about this.
dao data available from the sql definition language (ddl), so that you create the database, tables, records set and so on. In odbc no ddl.
Finally, when you need it, always on the basis of a direct call dao ole object.
mfc dao database class hierarchy
5 derived from the cobject mfc dao class (cdaoworkspace, cdaodatabase, cdaotabledef, cdaoquerydef and cdaorecordset) has all the features of the base class.
cdaoexception be derived from the cexception, with such advantages, including the display objects from the base dao errors error message capabilities.
As mentioned earlier, cdaorecordview be derived from the cformview while cformview has been derived from the cscrollview, and so on. Cdaorecordview class can see quickly and easily achieve the display based on the data form all the advantages possessed. In addition, support for cdaorecordview guide. The function of the class is essentially the same with crecordview class.
cdaofieldexchange class support the use of dao dao database class record field exchange (dfx) routine. If you want to create a custom dfx routine, as long as you can directly call the object.
mfc dao Database class
cdaoworkspace package dbengine object and workspace object. mfc dao class provides a very important fact that the work area. odbc database class does not simultaneously support multiple database connections.
Dao database transaction is in the class workspace (work area) level completed, not odbc class recordse (record set) level completed. A transaction may affect all open databases and records set, or you can isolate services so that it only affects the specified database, and so on.
Most of the time, you do not worry about the creation of workspace object. If you do not create, mfc on will you open a workspace object. If you need to do so, dao database class can support multiple workspaces.
Finally, you do not have to worry about beyond the scope of the work area or an object in the database before the completion of closed session. You can use the pointer to access the work area work area set, access to the database collection, as well as access to the database engine's attributes.
cdaodatabase
cdaodatabase is similar in architecture class based on odbc in cdatabase. cdaodatabase also packages a database connection. Because you do not always use odbc, so for most of the desktop data sources, data source location for the path on the expressions. cdaodatabase tabledef and querydef objects can be stored for your development provides a great convenience. cdaodatabase can use local and remote data sources. Later in this white paper, a data source available to you a list.
During the session of the database object has been continued. When necessary, explicitly close the database connection. Only in respect comparison, cdatabase class has 21 member functions, and cdaodatabase 26 member functions. The corresponding member functions in each are very similar, there are several in cdaodatabase no corresponding member function.
Not in the odbc database class and cdaotabledef corresponding class. tabledef objects allow you to check the database schema (structure), whether the table is the machine microsoft access table (base table) or a linked table. If dao directly open the external data source, you can add fields and indexes to them. If you link a table, you can check the structure, but can not change it. Records can be set as the basis for the table. This will make you gain several advantages, including the use of named members seek the quick search function.
Determine whether the use cdaotabledef by calling cdaotabledef:: canupdate edit data in the table. mfc dao field is responsible for managing and index your collection. Use cdaotabledef, you can choose whether to append the table to tabledefs collection; the use of all other objects, will be automatically appended.
Sql you used to retrieve records stored in the cdaoquerydef object. You can use the object store your questions about the data "problems" such as "how many customers did x dollars of business last month?". Can be retrieved or re-use of stored queries, according to one of the following three methods to use them:
By passing a pointer to the object and create a record set cdaoquerydef.
Direct implementation of the action queries that move or change data in the query. Action queries, including additional, delete, generate the table and update queries. Remove the query and update query to change the existing data; additional queries and generate query will move existing data.
Executive sqlpassthrough query: sql query is passed directly to the database server directly and will not be interrupted microsoft jet database engine sql statement. pass sql queries directly to your application using the database server to provide direct function of the ability.
In addition, cdaorecordset very similar to the class based on odbc in crecordset. Record set not only on the table, you can also dynamically set and based on a snapshot. Remember that record set means you have retrieved records, and through the data. Mobile and rolling method of data include seek (only for table type recordset), find and move operations, and absoluteposition and (if your data source supports it) bookmarks. Bookmark is the unique identifier may be returned by calling the identifier to the specified record.
mfc dao Database class most of the functionality is present in the cdaorecordset in. crecordset only 44 members of the function, compared to 91 cdaorecordset member functions. This additional functionality for the field values of the navigation performance, high-speed cache, setting and retrieval, and records set and retrieve property settings.
cdaorecordview and crecordview class has almost the same functionality. In addition, they are also a result of the advantage gained on cformview. Remember, Form view, as if the window client area the same stretch of the dialog template, with it, add the control and display field data is easy. appwizard and classwizard support forms-based data. If appwizard create the initial application, your database will be automatically bound to the column member variable.
For dao database class, exception error handling is slightly different. Class cdaoexception based dao ole object will return an error message. Most of the time, you can retrieve the error message than the usual class-based odbc error message received by many. In mfc, all errors are expressed as cdaoexception dao type of exception error.
When the capture of this type of exception errors, you can use cdaoexception member function is stored in the database engine from any errors in the collection dao error object to retrieve information. Each error occurs, will have one or more error objects into the errors collection. When another dao operation generated errors, errors collection is cleared, the new error object is placed in a collection of errors.
cdaofieldexchange class support the use of dao dao database class record field exchange (dfx) routine. If you are writing a custom data types of data exchange routines, the use of such; Otherwise, you will not directly use the class. dfx cdaorecordset object in your field data members and data sources on the current record to exchange data between the corresponding fields. dfx both directions in the management of the exchange, that exchange from the data source to data source with the exchange. For the preparation of custom dfx routine information, see technical note 53 (in the books online can be found under the mfc).
cdaofieldexchange occurred dao object provides the required record field exchange context information. cdaofieldexchange object supports a number of operations, including binding parameters and field data members, and the fields in the current record set various flags. dfx operation is the type of record in the data set of class members on the implementation of these types from the cdaofieldexchange defined in the enum fieldtype. Fieldtype possible values are:
For field data members cdaofieldexchange:: outputcolumn.
For parameter data members cdaofieldexchange:: param.
Here is based on the odbc database class picture, and you know the same. Based on the top of the table represents the odbc, odbc conversation with mfc classes. Each database odbc drivers sql call to explain, and for each data source to convert them. Many data sources with the appropriate driver appears in the diagram along the bottom to remind you odbc flexibility.
They adopted the ole communicate with dao, dao and jet database engine talk. jet database engine to have an independent dll, used with a variety of data sources to communicate with the desktop.
microsoft jet database engine can directly open, such as foxpro? database and paradox and other data sources. But as long as you do not need to change the structure of the data source, you can link to microsoft access these tables in the database, in fact, it would be more effective. This is the foxpro and paradox table (which is only an example) shows that the reasons in the two locations. Dotted line may be directly used to suggest when to open the data source, its efficiency will be lower.
The appearance of the linked table and work with the microsoft jet database, like any other table (although the connection to the remote data and remote data retrieval performance slightly different). Establishing and maintaining connections with the remote data source required information is stored in the table definition.
Instead, when you open the table directly, must be the beginning of each session to provide the connection information in order to establish a connection with the data source. Establish a connection with the remote data source the information they need is not stored in the microsoft jet database. To open the table directly, you must use cdaotabledef:: create, and must provide connection information (such as data source, user name, password and database name).
If you use dao database class, you can access the server via odbc type of database (such as microsoft sql server and oracle). Because it does not include all types of data sources.
Finally, a complete picture mfc database class. Please note, mfc odbc column and vertical bar on the assignment of the two databases that cluster as "either-or" decision-making. You can choose to access all types of data sources, but not mix the two sets of mfc database class.
odbc data source options like database
When you write applications mfc odbc class, you can connect to any data source (as long as you have it's odbc driver). odbc driver odbc driver manager and the operational use of these classes in your written application is transparent, but the performance of individual drivers will impact the application functionality.
Usually, mfc dynamic set (but not only roll forward set of records) requires odbc driver with two api consistency. If the data source driver found an api set, you can still use the read-only snapshots can be updated and rolled forward with the only set of records, but you can not use a dynamic set. However, if a driver supports expansion of extraction and key set-driven cursor, it can support the dynamic set.
odbc desktop driver pack 3.0 version supports two odbc api call sqlextendedfetch.
appwizard and classwizard data source automatically statically bind the column to your application's member variable. This is in your applications and data sources to establish a connection between the easiest way, but not the most flexible way. Can use a custom class cfieldexchange record field exchange (rfx) call to your application. Please see the "technical note 43: rfx routines", in order to obtain more information.
In addition, the database can be considered dynamically bound columns. In the most general level of perform the following steps:
◆ construct your main recordset object. Then, the pointer can be passed to an open cdatabase objects, or other methods can also be used to provide connectivity to the information set out in records.
◆ perform some steps to add columns dynamically.
◆ Open your main recordset.
◆ Record Select record assembly, and use the record field exchange (rfx) Binding "Static" column (mapped to the recordset field data members of the column) and the dynamic column (mapping the distribution of additional storage to your column).
dao database type of data source options
Now we want to talk about is that you can use dao database connection data source class, out of the static binding, dynamic binding column, and record double-buffering.
Since the jet microsoft access database is a database of this machine, you naturally can access them as quickly. microsoft access 97 with the database format is dao 3.5 version of the native database format. If you use microsoft access 97 database, the performance will be most rapid.
jet provides a separate dll microsoft jet 1.x and 2.0 database access. Storage Engine and format is microsoft jet 3.0 completely revised edition. If the change and have many, microsoft jet 3.0 Version 2.0 database will be treated as external isam, this will affect performance. Therefore, this is to promote and you should consider upgrading to microsoft access database, the reasonable cause.
In addition, you can visit the isam database can be installed and odbc data source. isam (continuous access method based on index) database, such as the foxpro and dbase, you can directly open, you can also link to access the database to achieve optimal performance. The following is dao to access a list of data sources:
◆ microsoft foxpro of 2.0,2.5 and 2.6. In the 3.0 version can import and export data, but can not create object.
◆ dbase iii, dbase iv, and dbase 5.0
◆ paradox of the 3.x, 4.x and 5.x version
◆ btrieve of 5.1x and 6.0
◆ microsoft excel of 3.0,4.0,5.0,7.0 and 8.0 worksheet
◆ lotus wks, wk1, wk3, wk4 Spreadsheet
Text File
Remember, microsoft access for 1.x, 2.0 and 7.0 databases fall into this category.
Access via odbc odbc data source, such as sql server and oracle, so you can choose to use these data sources dao. An odbc data source can come from any dbms, dbms as long as you have the appropriate odbc driver. For the visual c + + 2.0 or later, you need 32-bit odbc driver (except win32 but in win32 need 16-bit odbc driver). The following is the version of visual c + + odbc driver included in the list.
◆ sql server
◆ microsoft access
◆ microsoft foxpro
◆ microsoft excel
◆ dbase
◆ paradox
◆ text file
microsoft desktop database drivers 3.0 version (which contains the final list of six) for these data sources to provide the best performance. The only 32-bit drivers.
External data source (such as sql server) link to microsoft access table is the most effective method of dealing with data access. In the application to connect to the remote data source before the application must first ensure that users can access remote data, and ensure the correct design for your application to remote data sources to solve the security problem. In addition, you must also ensure that the application correctly and is case-sensitive exchange of data sources and ensure proper initialization can be installed isam, to be used to access the data source you want. Finally, you must check your code, to ensure access to non-jet data source, the code does not use the microsoft jet data source-specific object or call.
The most simple way to set the link is: access to microsoft access. If you are using microsoft access 2.0 version, use the "file" / "Add table" command; If you are using microsoft access 95, and microsoft access 97, then use the "file" / "Get External Data" / "link table" command .
Connection information is stored in the base table that you are using (. Mdb) in. If you move the location of external data, it is necessary or in the microsoft access code calls from cdaotabledef:: refreshlink, re-establish the link.
dao record field exchange mechanism with the database based on odbc rfx class have the same work. Recordset object's field data members, if linked, would form a "edit buffer" to preserve a record of the selected column. When the record set for the first time to open and to read the first record, dfx would be each of the selected column with the appropriate members of the address field data binding (association) together. When the recordset update a record, dfx call the dao to the database engine to send the appropriate command. dfx field data using its members to understand the contents of the specified data source to be written in the column (field).
Wizard supports static binding column. You can add your own dfx call, as the class-based as odbc. First, for each bound field and parameters of the membership must be added to the cdaorecordset derived class. Next, cdaorecordset:: dofieldexchange should be replaced. Please note that members of the type of data is very important. It should work with the data from the database field in the match, or at least can be converted to that type. mfc technical # 53 in more detail about this process.
cdaofieldexchange class support the use of dao dao database class record field exchange (dfx) routine. If you are writing a custom data types of data exchange routines, use the class. cdaofieldexchange occurred dao object provides the required record field exchange context information. cdaofieldexchange object supports a number of operations, including binding parameters and field data members, and the fields in the current record set various flags.
dao Database class dynamic binding
If you can use the class-based dynamic binding odbc out, support for such behavior will not be built on the mfc class. Dynamic binding dao database will be built in class, and is pretty easy.
You can also do some other things to optimize performance, as part of the record rather than retrieve the entire record. Later in this presentation, we will provide some methods to optimize applications.
dfx and dynamic binding are not mutually exclusive options. Dao database by category, you can call the mixed static and dynamic binding to achieve maximum efficiency.
dao database class in the double buffer
The cdaorecordset class in mfc, double buffering is a record set when the current record changes simplifying the detection mechanism. When adding new records and edit existing records, dao recordset to your use of double buffering will reduce the workload required. By default, your mfc dao recordset to retain the second copy of the edited buffer (recordset field data members of the class, all the copy; dao "help" in the appropriate buffer known as "copy buffer"). When you make changes to the data members, mfc will take them with a copy ("double buffer") comparison to detect change.
The alternative method of double buffering - does not retain a copy of the data - when editing the current record field that requires you to other function calls.
Double buffering has been a class-based part of the database odbc. For dao database type, if necessary, you can disable the mechanism to improve efficiency.
The mechanism of the main switch, called m_bcheckcachefordirtyfields, dirty means "has been changed." If this switch is on (open), on all or part of the field can enable double buffering. If the main switch is off (off), it will disable the double buffering mechanism.
You most want to shut down its field, including double-buffering Notes field, image field, and the other blob (binary large objects).
dao sdk contains a number of c + + database classes, these classes are independent of each other, but different from the mfc dao database class. The c + + class to encapsulate a single dao object hierarchy. Although you can dao sdk c + + classes and mfc dao database mixed, but the dao sdk c + + class does not follow the guidelines for operator overloading mfc, and in the mixed use of these classes, you must be careful.
mfc odbc database class to use microsoft access and visual basic records in the set model. You can use the built-in class member functions to filter the records, sorting, scrolling and other treatments. Odbc drivers will affect the basis of specific application functionality. Therefore, to make portability more, your application must be more universal, or must rely on lower-level odbc functions. If necessary, you can directly call the odbc, to complete specific tasks. Now, let us examine the collection of a single class.
Mfc so as to use, if necessary, can call the base api (in this case for the odbc).
A cdatabase object represents a connection to the data source, through this connection you can manipulate the data source. Data source refers to some of the database management system (dbms) instance as the host of the specified data. Specific examples include microsoft sql server, microsoft access, borland dbase and xbase. You can own applications while allowing one or more cdatabase object is active, and can maintain a database object to the multiple connections.
Crecordset object represents a data source selected from a record collection. Known as the "Record Set" crecordset objects available in three forms: the dynamic set, snapshot, dynamic recordset. Dynamic set of updates with other users synchronized set of records. Snapshot is a static recordset, it reflects the state of the database snapshot snap. Dynamic record set similar to the dynamic set, but usually only for microsoft sql server. Each form represents a record set in the open record established at the time set, but when you set within a dynamic or dynamic record set in rolling to a record, it reflected later by another user or your application to other records set on the record changes.
mfc 4.2 to bookmark the form of crecordset class to add new navigation features, and add the logo and navigation ability to record absoluteposition. Bookmark is the unique identifier, the identifier can be returned by calling the designated records.