Launch Microsoft Visual Studio.
Select File|New |Project from the menu.
The New Project dialog box appears.
Enter DbQuery for the Project name and <stingray-installdir>\Samples\Grid\Tutorial\DbQuery\Mygrid for the Location.
Select MFC Application from the list box.
Click OK.
The MFC Application Wizard dialog box appears.
Select Multiple documents for MFC AppWizard under Application type.
Click Database Support.
Select Header files only for MFC Application Wizard under Database Support and Client Type as ODBC.
Accept the defaults for all other steps. You can scan through the settings by clicking the links, or click Finish to create the project.
This step specifies that the application should use Objective Grid as a shared library. You should skip this step if you want to link statically to the Objective Grid libraries.
Select Solution Explorer from the View menu.
Right click on DbQuery and select Properties to get the Property Pages window.
The Property Pages window appears.
Choose All Configurations in the Configuration: combo box in the top-left corner of the dialog.
Select the C/C++ tab and add _GXDLL to the Preprocessor definitions. This step indicates that the application will link dynamically to Objective Grid. Do not specify _GXDLL if you wish to link to Objective Grid as a static library. _GXDLL is similar to the _AFXDLL preprocessor in that _AFXDLL specifies that the application will link to MFC as a dynamic library.
Click OK.
Gxall.h is the core Objective Grid header file. Once this file is added, all required classes will be available to you inside your application. This is similar to including afx*.h. Gxodbc.h is the Objective Grid ODBC header file.
Open stdafx.h
Add the following lines at the end of the file:
#include "grid\gxall.h" #include "grid\gxodbc.h" |
Save and close stdafx.h.
Including the Objective Grid resource header file in your application's resource script gives your application access to the Objective Grid Resources. When linking statically to the Objective Grid libraries, it is also necessary to include the Objective Grid resource script. The following steps demonstrate how to add the Objective Grid resource files to your project.
Select Resource View from the View menu.
Right-click on the root of the Resource View tree (DbQuery.rc).
A context menu pops up.
Choose Resource Includes.
The Resource Includes dialog box appears.
Add the following line to the Read-only symbol directives box.
#include "grid\gxresrc.h" |
Add the following line to the end of the Compile-time directives box.
#include "grid\gxres.rc" |
Click OK.
A message box pops up warning you that the "Directive text will be written verbatim into your resource script and may render it uncompilable." This is fine. You can safely choose OK.
GXInit() initializes resources and variables used by Objective Grid. It should be called from your application's InitInstance() to be sure Objective Grid is initialized before calling any Objective Grid functions. GXInitODBC() initializes the ODBC support for Objective Grid. For more information about GXInit() or GXInitODBC(), please refer to the Objective Grid Class Reference.
Open dbquery.cpp and search for InitInstance().
Add the following lines to the beginning of InitInstance():
// These calls will initialize the grid library GXInit(); GXInitODBC(); |
Use the ODBC Data Source Administrator to register the stdreg32.mdb database file as Student Registration. Section 8.2.8, "Add a CRecordset Derived Class," uses this data source, so you'll need to register this data source before continuing with the tutorial.
This section gives you step by step instructions. If you don't need the details, skip this section.
Locate the stdreg32.mdb file in the <stingray_installdir>\Samples\Grid\Database folder. Make sure your copy of stdreg.mdb is not read-only.
If Windows 2000 is your operating system, start the ODBC Data Source Administrator by double-clicking the Data Sources (ODBC) icon in the Control Panel\Administrative Tools.
Click the tab corresponding to the category of data source you want to register. For example, choose the System DSN tab if the data source should be visible to all users on the machine including NT services. Figure 85, Figure 86, and Figure 87 show the difference between the different categories of ODBC data sources.
Click the Add button.
Select the driver that corresponds to Access (*.mdb).
Click Finish.
In the ODBC Microsoft Access Setup dialog, type Student Registration as the Data Source Name. (You don't need to add a description.)
Click Select.
Navigate to the samples\grid\tutorial folder and select the name of the database (stdreg32.mdb).
Click OK.
Make sure the Student Registration database shows up correctly.
Click OK.
The following steps use the Student Registration database (stdreg32.mdb). Make sure that you've registered the database as described in Section 8.2.7, "Register a Database."
Select Class View from the View menu.
Right click on DbQuery and click Add, then Class.
Select MFC from the tree node, and MFC ODBC Consumer from the list box.
Click the Data Source... button and select Student Registration under the Machine Data Source tab.
Press OK.
The Login dialog box for the selected DataSource appears.
Enter the Login and Password if applicable.
Click OK to open the Select Database Object dialog box.
Select the Student table and press OK.
Click OK.
Enter CStudentSet in the Class: field, StudentSet.h in the .h file: field, and StudentSet.cpp in the .cpp file: field.
Place a checkmark in the Bind all columns check box, and select the Dynaset radio button.
Click Finish to generate the necessary files.
The wizard may put an error message in the StudentSet.cpp file as follows:
#error Security Issue: The connection string may contain a password //The connection string below may contain plain text //passwords and/or other sensitive information. Please remove //the #error after reviewing the connection string for any //security related issues. You may want to store the password //in some other form or use a different user authentication. This error message can be removed for the purpose of this tutorial. |
In this step, the application's view class is changed from a CView derivative to a CGXGridView derivative.
Open DbQueryView.h.
Add the line:
class CStudentSet; |
as a forward declaration before the class declaration and add:
CStudentSet* m_pSet; |
as a member variable to CDbqueryView's attributes section.
The completed code should look like this:
// dbquevw.h : interface of the CDbqueryView class // ///////////////////////////////////////////////////////// class CStudentSet; class CDbqueryView : public CView { protected: // create from serialization only CDbqueryView(); GRID_DECLARE_DYNCREATE(CDbqueryView) // Attributes public: CDbqueryDoc* GetDocument(); CStudentSet* m_pSet; ... |
Change the derivation of the view by replacing all occurrences of CView with CGXRecordView.
Open DbQueryView.cpp.
Add:
#include "studentset.h" |
The completed code should look like this:
// DbQueryView.cpp : implementation of the CDbQueryView class // #include "stdafx.h" #include "DbQuery.h" #include "DbQueryDoc.h" #include "DbQueryView.h" #include "studentset.h" ... |
Replace all occurrences of CView with CGXRecordView.
Change the view's constructor and destructor as follows:
CDbQueryView::CDbQueryView() { m_pSet = new CStudentSet; } CDbQueryView::~CDbQueryView() { delete m_pSet; } |
Add a new method, OnGetRecordset():
CRecordset* CDbQueryView::OnGetRecordset() { return m_pSet; } |
Also insert the definition for this method in DbQueryView.h:
virtual CRecordset* OnGetRecordset(); |
Modify OnDraw(), OnBeginPrint(), and OnEndPrinting():
void CDbQueryView::OnDraw(CDC* pDC) { CDbQueryDoc* pDoc = GetDocument(); ASSERT_VALID(pDoc); CGXRecordView::OnDraw(pDC); } void CDbQueryView::OnBeginPrinting(CDC* pDC, CPrintInfo* pInfo) { CGXRecordView::OnBeginPrinting(pDC, pInfo); } void CDbQueryView::OnEndPrinting(CDC* pDC, CPrintInfo* pInfo) { CGXRecordView::OnEndPrinting(pDC, pInfo); } |
At this point, you can compile and run the DbQuery application.
Compile the application and run it.
Copyright © Rogue Wave Software, Inc. All Rights Reserved.
The Rogue Wave name and logo, and Stingray, are registered trademarks of Rogue Wave Software. All other trademarks are the property of their respective owners.
Provide feedback to Rogue Wave about its documentation.