views:

269

answers:

3

I still remember in Delphi, developer can just make the UI(textbox, listbox...) directly connect to database, and then when user click a button, just call the post action, then the data will be saved automatically.

What I want to know is that is there any similar mechanism in MFC? Or I can use GetDlgItem(...).Text and then use this value to save to database ?

Or any other suggestions will be appreciated.

A: 

To be fair on Delphi these are specialized widgets, not the ordinary GDI textbox etc, but controls with an additional database aware layer that are connected to dataset and tables.

.NET has something similar concepts too, don't know about MFC

Marco van de Voort
+1  A: 

In VC++ , you have to use Microsoft ActiveX Data Object Library (ADO typelib) .

To store data you can follow these steps:

1.Retrive data from all controls 
2.Validate the data retrived
3.Use sql query to store the data to database.

You can use ODBC API which is independent of any database management system.

http://msdn.microsoft.com/en-us/library/ms714562(VS.85).aspx

http://www.odbc.net/api/index.shtml

Ashish
There is no database independant layer? What if you need ODBC ?
Marco van de Voort
A: 

MFC's abstraction of data in Doc/View/Frame is in CDocument. When you save the document, MFC prompts the user for the file name if the file does not have a saved path, then construct a CArchive on the file and triggers CDocument::Serialize. You can store the connection string in your document class and use it to save data in CDocument::Serialize.

If you have a file based database, it is easier to integrate. Override CDocument::OnNewDocument to create a new file based database for the document, and override CDocument::OnOpenDocument to read from existing database. If you don't have a file based database, you can suppress the file dialog with a CDocument::SaveModified override that saves the data and clears the modified flag.

Sheng Jiang 蒋晟