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

Adding and Deleting Records

As soon as you can add and delete records from a database table, you have a full-featured program for manipulating a flat (that is, not a relational) database. In this case, the "flat database" is the Employees table of the Department Store relational database. Adding and deleting records in a database table is an easier process than you might believe, thanks to Visual C++'s CRecordView and CRecordSet classes, which provide all of the member functions that you need to accomplish these common database tasks. Follow the steps that come next to include add and delete commands in the Employee application.

1. Choose the ResourceView tab, open the Menu folder, and double-click the IDR_MAINFRAME menu ID.

2. Open the Record menu in the editor, and then double-click the blank menu item at the bottom of the menu. The Menu Item Properties property sheet appears.

3. In the ID edit box, enter ID_RECORD_ADD, and in the Caption box, enter &Add Record. When you press Enter, the menu editor adds the new command to the Record menu.

4. In the next blank menu item, add a delete command with the ID ID_RECORD_DELETE and the caption &Delete Record.

5. In the ResourceView pane, open the Toolbar folder, and then double-click the IDR_MAINFRAME ID. The application's toolbar appears in the resource editor.

6. Click the blank toolbar button to select it, and then use the editor's tools to draw the icon (it's supposed to be blue) on the button.

7. Double-click the new button in the toolbar. The Toolbar Button Properties property sheet appears. Choose ID_RECORD_ADD in the ID box.

8. Create a new toolbar button that displays a red minus sign, giving the button the ID_RECORD_DELETE ID. Move the Add and Delete buttons to a position before the Help (question mark) button.

9. Choose the View, ClassWizard command from the menu bar, or just press Ctrl+W on your keyboard. The MFC Class Wizard property sheet appears. (Choose the Message Maps tab if necessary.)

10. Set the Class Name box to CEmployeeView, click the ID_RECORD_ADD ID in the Object IDs box, and then double-click COMMAND in the Messages box. The Add Member Function dialog box appears.

11. Click the OK button to accept the default name for the new function. The function appears in the Member Functions box.

12. Add a member function for the ID_RECORD_DELETE command in the same way shown in steps 10 and 11. Then click the OK button on the MFC ClassWizard property sheet to finalize your changes. The ClassWizard property sheet closes.

13. Open the EmployeeView.h file and, in the Attributes section of the class's declaration, add the following lines:

protected:

BOOL m_bAdding;

14. Open the EmployeeView.cpp file, and then add the following line to the class's constructor, right after the comment TODO: add construction code here:

m_bAdding = FALSE;

15. Still in EmployeeView.cpp, find the OnRecordAdd() function, and add the code shown in Listing 12.1 below.

Listing 12.1 Code for the OnRecordAdd() Function

m_pSet->AddNew();

m_bAdding = TRUE;

CEdit* pCtrl = (CEdit*)GetDlgItem(IDC_EMPLOYEE_ID);

int result = pCtrl->SetReadOnly(FALSE);

UpdateData(FALSE);

16. Use ClassWizard to override the OnMove() member function in the CRecordView class. Click the Edit Code button to jump to the function in the text editor.

17. Replace the default code in the OnMove() function with the code shown in Listing 12.2.

Listing 12.2 Code for the OnMove() Function

if (m_bAdding)

{ m_bAdding = FALSE;

UpdateData(TRUE);

if (m_pSet->CanUpdate())

m_pSet->Update();

m_pSet->Requery();

UpdateData(FALSE);

CEdit* pCtrl =(CEdit*)GetDlgItem(IDC_EMPLOYEE_ID);

pCtrl->SetReadOnly(TRUE);

return TRUE;

}

else return CRecordView::OnMove(nIDMoveCommand);

18. Add the code shown in Listing 12.3 to the OnRecordDelete() function.

Listing 12.3Code for the OnRecordDelete() Function

m_pSet->Delete();

m_pSet->MoveNext();

if (m_pSet->IsEOF())

m_pSet->MoveLast();

if (m_pSet->IsBOF())

m_pSet->SetFieldNull(NULL);

UpdateData(FALSE);

You've now modified the Employee application so that it can add and delete, as well as update, records. After compiling the application, run it by selecting the Build, Execute command from Developer Studio's menu bar or by pressing Ctrl+F5 on your keyboard. When you do, you see the Employee application's main window, which doesn't look any different than it did in the previous section. Now, however, you can add new records by clicking the Add button on the toolbar (or by selecting the Record, Add Record command in the menu bar) and delete records by clicking the Delete button (or by clicking the Record, Delete Record command).

When you click the Add button, the application displays a blank record. Fill in the fields for the record, and then, when you move to another record, the application automatically updates the database with the new record. To delete a record, just click the Delete button. The current record (the one on the screen) vanishes, each field being filled in with the symbol "<deleted>."

Examining the OnRecordAdd() Function

You now might be wondering how the C++ code that you added to the application works. When you click the Add button or choose the Record, Add command MFC routes program execution to the OnRecordAdd() command handler. In that function, the program first calls the CEmployeeSet object's AddNew() member function, like this:

m_pSet->AddNew();

The AddNew() member function sets up a blank record for the user to fill in. The new blank record doesn't appear on the screen, however, until the view window's UpdateData() function is called.

After creating the new record, the program sets the Boolean variable m_bAdding to TRUE, which indicates to the program that the user is in the process of adding a new record:

m_bAdding = TRUE;

Now, because the user is entering a new record, he or she needs to be able to change the contents of the Employee ID field, which is currently set to read-only. To change the read-only status of the control, the program first gets a pointer to the control and then calls the control's SetReadOnly() member function, like this:

CEdit* pCtrl = (CEdit*)GetDlgItem(IDC_EMPLOYEE_ID);

int result = pCtrl->SetReadOnly(FALSE);

The SetReadOnly() member function changes the read-only style of a control. When the function's single argument is FALSE, the control is enabled for normal editing. When the function's argument is TRUE, the control is set to its read-only state, which prevents the user from changing the value displayed in the control.

Finally, as I mentioned previously, the program must call the view window's UpdateData() member function to display the new blank record, like this:

UpdateData(FALSE);

When the UpdateData() function's argument is FALSE, data is transferred from the record set to the control. When the argument is TRUE, data is transferred from the control to the record set.

Examining the OnMove() Function

Now that the user has a blank record on the screen, he or she can start filling in the edit controls with the necessary data. To actually add the new record to the database, the user must move to a new record, an action that forces a call to the view window's OnMove() member function. Normally, OnMove() does nothing more than display the next record. However, because you overrode the function and added your own code, you've enabled the program to save the new record.

When OnMove() is called, the first thing that the program does is check the Boolean variable m_bAdding to see whether the user is in the process of adding a new record:

if (m_bAdding)

If m_bAdding is FALSE, the body of the if statement is skipped, and the else clause is executed. In the else clause, the program calls the base class's (CRecordView) version of OnMove(), which performs the default behavior for moving to the next record. If m_bAdding is TRUE, the body of the if statement is executed. There, the program first resets the m_bAdding flag:

m_bAdding = FALSE;

Then the program calls UpdateData() to transfer data out of the view window's controls:

UpdateData(TRUE);

A call to the record set's CanUpdate() method determines whether it's okay to update the data source, after which a call to the record set's Update() member function adds the new record to the data source:

if(m_pSet->CanUpdate())

m_pSet->Update();

To rebuild the record set, the program must call the record set's Requery() member function, like this:

m_pSet->Requery();

A call to the view window's UpdateData() member function transfers new data to the window's controls:

UpdateData(FALSE);

Finally, the program sets the Employee ID field back to read-only so that the user can no longer change its contents:

CEdit* pCtrl = (CEdit*)GetDlgItem(IDC_EMPLOYEE_ID);

pCtrl->SetReadOnly(TRUE);

Examining the OnRecordDelete() Function

When the user clicks the Delete button (or chooses the Delete Record command from the Record menu), the OnRecordDelete() function gets called. In that function, deleting the record is just a matter of calling the record set's Delete() function:

m_pSet->Delete();

As soon as the record is deleted, however, the program should display another record in its stead. That's where the call to the record set's MoveNext() function comes in:

m_pSet->MoveNext();

The MoveNext() function moves the record set forward to the next record. A problem might arise, though, when the deleted record was in the last position or when the deleted record was the only record in the record set. A call to the record set's IsEOF() function will determine whether the record set was at the end. If the call to IsEOF() returns TRUE, the record set needs to be repositioned on the last record, which it is currently beyond. The record set's MoveLast() function takes care of this task. The code looks like this:

if (m_pSet->IsEOF())

m_pSet->MoveLast();

When the last record has been deleted from the record set, the record pointer will be at the beginning of the set. The program can test for this situation by calling the record set's IsBOF() function. If this function returns TRUE, the program sets the current record's fields to NULL, like this:

m_pSet->SetFieldNull(NULL);

Finally, the last task is to update the view window's display:

UpdateData(FALSE);

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