Use OLE DB
5.1 Overview
The existence of ole db provides users with a unified method to access all different types of data sources. Ole db can be converted in different data sources. With ole db, client developers only need to focus on a few details when accessing data, rather than understanding a large number of access protocols for different databases.
Ole db is a set of ActiveX interfaces that access data through COM interfaces. This ole db interface is quite common enough to provide a unified means of accessing data, regardless of the method used to store data. At the same time, ole db also allows developers to continue to take advantage of the advantages of the basic database technology, without the need to use these advantages to move data out.
5.2 use the ole db data user program with ATL
A large amount of code is required to design database applications directly using ole db objects and interfaces. To simplify programming, Visual C ++ provides an ATL template for designing ole db data applications and data providers.
Using the ATL template, you can easily combine ole db with MFC to simplify complex programming such as database parameter query. The database classes provided by MFC make ole db programming more object-oriented. The ATL templates provided by Viual C ++ for ole db can be divided into templates for data providers and templates for data usage programs.
To create a data application using an ATL template, follow these steps:
1. Create an application framework
2. Add the template class generated by ATL
3. Use the generated data access object in the Application
5.3 use ole db data using programs without ATL
Using the ATL template to generate data is relatively simple to use, but it is not widely applied and cannot dynamically adapt to database changes. The following describes how to directly use the mfc ole db class to generate a data application.
Template usage
Ole db data user templates are composed of some templates, including the following templates. Some common classes are described below.
1. Session
CDataSource class
The CDataSource class corresponds to the data source object of ole db. This class represents the connection between the ole db data provider and the data source. A session object can be generated only after a connection to the data source is established. You can call Open to Open a connection to the data source.
CSession
The CSession object represents a separate database access session. A data source object generated using the CDataSource class can create one or more sessions. To generate a session object on the data source object, you must call the Open () function to Open it. At the same time, session objects can also be used to create transaction operations.
CEnumeratorAccessor class
The CEnumeratorAccessor class is used to access information of available data providers in the rows generated by the enumerator query. It can provide currently available data providers and visible accessors.
2. accessors
CAcessor class
The CAccessor class represents the type of the accessor. This class can be used when you know the database type and structure. It supports multiple accessors for a row set, and the buffer for storing data is allocated by the user.
CDynamicAccessor class
The CDynamicAccessor class is used to dynamically create accessors when the program is running. When the system is running, the column information can be dynamically obtained from the row set, and the accessors can be dynamically created based on this information.
CManualAccessor class
In the CManualAccessor class, you can bind columns to variables or bind parameters to variables when the program is running.
3. Row set class
CRowSet class
The CRowSet class encapsulates the row set object and corresponding interfaces, and provides methods for querying and setting data. You can use functions such as Move () to record data movement, GetData () to read data, and Insert (), Delete (), and SetData () to update data.
CBulkRowset class
The CBulkRowset class is used to retrieve multiple row handles or operate on multiple rows in a single call.
CArrayRowset class
The CArrayRowset class provides data access using array subscript.
4. Command class
CTable class
The CTable class is used for simple access to the database, and the row set is obtained using the data source name to obtain data.
CCommand class
The CCommand class is used for data sources that support commands. You can use the Open () function to execute SQL commands, or the Prepare () function to Prepare commands first. For data sources that support commands, you can improve the flexibility and robustness of the program.
Add the following code to the stdafx. h header file.
# Include <atlbase. h>
Extern CComModule _ Module;
# Include <atlcom. h>
# Include <atldbcli. h>
# Include <atldbsch. h> // if you are using schema templates
Add the following code to the stdafx. cpp file.
# Include <atlimpl. cpp>
CComModule _ Module;
Determines the type of access program and row set.
Get Data
After opening the data source, session, and row set object, you can get the data. The data type obtained depends on the access program used, and columns may need to be bound. Follow these steps.
1. Use the correct command to open the row set object.
2. If you use CManualAccessor, bind it to the corresponding column before use. To bind a column, use the GetColumnInfo function as follows:
// Get the column information
ULONG ulColumns = 0;
DBCOLUMNINFO * pColumnInfo = NULL;
LPOLESTR pStrings = NULL;
If (rs. GetColumnInfo (& ulColumns, & pColumnInfo, & pStrings )! = S_ OK)
AfxThrowOLEDBException (rs. m_pRowset, IID_IColumnsInfo );
Struct MYBIND * pBind = new MYBIND [ulColumns];
Rs. CreateAccessor (ulColumns, & pBind [0], sizeof (MYBIND) * ulColumns );
For (ULONG l = 0; l <ulColumns; l ++)
Rs. AddBindEntry (l + 1, DBTYPE_STR, sizeof (TCHAR) * 40, & pBind [l]. szValue, NULL, & pBind [l]. dwStatus );
Rs. Bind ();
3. Use a while loop to retrieve data. In a loop, call MoveNext to test whether the returned value of the cursor is S_ OK, as shown below:
While (rs. MoveNext () = S_ OK)
{
// Add code to fetch data here
// If you are not using an auto accessor, call rs. GetData ()
}
4. In the while LOOP, data can be obtained through different access programs.
1) if you are using the CAccessor class, you can directly access it by using their data members. As follows:
2) if you are using the CDynamicAccessor or CDynamicParameterAccessor class, you can use the GetValue or GetColumn function to obtain data. You can use GetType to obtain the data type used. As follows:
While (rs. MoveNext () = S_ OK)
{
// Use the dynamic accessor functions to retrieve your
// Data
ULONG ulColumns = rs. GetColumnCount ();
For (ULONG I = 0; I <ulColumns; I ++)
{
Rs. GetValue (I );
}
}
3) If CManualAccessor is used, you can specify your own data members and bind them. You can directly access it. As follows:
While (rs. MoveNext () = S_ OK)
{
// Use the data members you specified in the callto
// AddBindEntry.
Wsprintf ("% s", szFoo );
}
Determines the Data Type of the row set.
The data type is determined during running. Dynamic or manual access is required. If you use a manual access program, you can use the GetColumnInfo function to obtain the column information of the row set. Here we can get the data type.
5.4 conclusion
Because there are multiple data sources, the only way to access and manage these data is through some similar mechanisms, such as ole db. The advanced ole db structure is divided into two parts: customer and provider. The customer uses the data generated by the provider.
Just like most other COM-based structures, ole db developers need to implement many interfaces, most of which are template files.
When a customer object is generated, you can use the ATL object wizard to point to a data source to create a simple customer. The ATL object wizard will check the data source and create a database client proxy. From there, you can use standard browse functions through the ole db customer template.
When a provider is generated, the wizard provides a good start. They only generate a simple provider to list files in a directory. Then, the provider template contains all the supplementary content supported by ole db. With this support, you can create an ole db provider to implement row set locating policies, read and write data, and create bookmarks.