Добавил:
Upload Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
SSW_8_11.doc
Скачиваний:
0
Добавлен:
01.05.2025
Размер:
5.22 Mб
Скачать

Accessing a Database

Relational databases are accessed using some sort of database scripting language. The most commonly used database language is SQL, which is used, not only to manage databases on desktop computers, but also on the huge databases used by banks, schools, corporations, and other institutions with sophisticated database needs. Using a language like SQL, you can compare information in the various tables of a relational database and extract results that are made up of data fields from one or more tables combined.

Learning SQL, though, is a large task, one that is beyond the scope of this book (let alone this chapter). In fact, entire college-level courses are taught on the design, implementation, and manipulation of databases.

12.2 Mfc odbc Classes

When you create a database program with Visual C++'s AppWizard, you end up with an application that draws extensively upon the various ODBC classes that have been incorporated into MFC. The most important of these classes are CDatabase, CRecordset, and CRecordView.

AppWizard automatically generates the code needed to create an object of the CDatabase class. This object represents the connection between your application and the data source that you access. In most cases, using the CDatabase class in an AppWizard-generated program is transparent to you, the programmer. All of the details are handled by the framework.

AppWizard also generates the code needed to create a CRecordset object for the application. However, how you plan to use this object depends on whether you need to write code that calls the object's member functions. (Unless you're doing nothing more with the database than viewing its content, you need to call CRecordset member functions in your program.) The CRecordset object represents the actual data that's currently selected from the data source. The CRecordset object's member functions enable you to manipulate the data from the database in various ways.

Finally, the CRecordView object in your database program takes the place of the normal view window that you're accustomed to using in AppWizard-generated applications. A CRecordView window is special in that it's kind of like a dialog box that's being used as the application's display. This dialog box[en]type of window retains a connection to the application's CRecordset object, hustling data back and forth between the program, the window's controls, and the record set. When you first create a new database application with AppWizard, it's up to you to add edit controls to the CRecordView window. These edit controls must be bound to the database fields that they represent, so the application framework knows where to display the data you want to view. In the next section, you see how these various database classes fit together, as you build the Employee application step by step.

Although creating a simple ODBC database program is easy with Visual C++, there are a number of steps that you must complete:

1. Register the database with the system.

2. Use AppWizard to create the basic database application.

3. Add code to the basic application to implement features not automatically supported by AppWizard.

In the following sections, you'll see how to perform the preceding steps as you create the Employee application, which enables you to add, delete, update, sort, and view records in the Employees table of the sample Department Store database.

Соседние файлы в предмете [НЕСОРТИРОВАННОЕ]