|
|
|
A simple database applicationIt's very easy to connect to a database and get, edit, add and delete records in a table of the database using MFC classes, CDatabase and CRecordset. In this article, you will learn how to use the Visual Studio application wizard to create a simple database application which connect to a Access *.mdb database. Because CDatabase and CRecordset uses ODBC interface to connect to work with a database, we first have to create an ODBC data source. Here are the steps for Windows XP.
Now you have created the ODBC data source in your computer. Lets now create our simple application which connects to our newly created data source.
The application you get will have a dialog form created for you. You can locate it from the resource view. Add controls to this form and make it similar to this one.
It is better if you use some meaningful names to the control ids of the edit boxes. I have used IDC_USERNAME, IDC_FIRSTNAME, IDC_LASTNAME, IDC_EMAIL, IDC_COUNTRY. You can change the control id of edit box from the properties of the edit box. Now, go to view class of your application and locate DoDataExchange() function. In this sample application, it is located in the file "db accessView.cpp". In this function, you can "bind" the fields of our form to the fields in the table of our database. A CRecordset variable (m_pSet) is created for us by the application wizard. This variable corresponds to the table in our database and it has a mamber variable for each field in the table. Here is the DoDataExchange function. void CdbaccessView::DoDataExchange(CDataExchange* pDX) All the fields in our databse table are strings. So we have use DDX_FieldText function for bind the variable to the text boxes in our form. There are other functions such as DDX_FieldCBIndex to synchronize with other data types. You will have to include "resource.h" header to the cpp file with above code to compile the project. Now, if you compile the application, you will get an error. If you go to the location of error, you will see #error preprocessor directive telling that the passwords may be in the connection string . just comment that line. Now you should be able to compile the project. Now, we have created a simple application so easily which can read and edit the data in a database table. You will get a toolbar to go through the records in the table. Now, lets see how to we can add a new record to the database table. You can see a button on our form to add a new record to the database table. here is the handler of that button. void CdbaccessView::OnBnClickedButton1() I have created a dialog to display when that button is clicked. If you do not know how to create a dialog, see creating custom dialogs in MFC section. The class of this dialog is "adduserdlg". I have created a variable form that class and have called DoModal function to display the dialog. If user fills the data and clicks OK in that dialog, we add the new record to the database table. To add a new record to the database table, we first call the AddNew() function of the CRecordset variable corresponds to that table. Then we assign values to the each field in the table. After that, we call Update() and Requery() fucntions to save the data to the database. You can download the source code of the sample application here. For the example application to work properly, you have to copy database.mdb to C: drive and create an ODBC data source with name 'test' from that file. Some useful functions in CRecordsetMoveNext() MoveLast() |