开发者

How do you save data in MFC?

开发者 https://www.devze.com 2022-12-14 19:25 出处:网络
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

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 ot开发者_JS百科her suggestions will be appreciated.


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


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


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消