MFC中如何保存数据?
我还记得在Delphi中,开发人员可以让UI(文本框,列表框...)直接连接到数据库,然后当用户单击按钮时,只需调用post操作,然后数据就会自动保存。
我想知道MFC中有没有类似的机制?或者我可以使用 GetDlgItem(...).Text 然后使用该值保存到数据库?
或者任何其他建议将不胜感激。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
在 VC++ 中,您必须使用 Microsoft ActiveX 数据对象库(ADO typelib)。
要存储数据,您可以按照以下步骤操作:
您可以使用独立于任何数据库管理系统的ODBC API。
http://msdn.microsoft.com/en-us /library/ms714562(VS.85).aspx
http://www.odbc .net/api/index.shtml
In VC++ , you have to use Microsoft ActiveX Data Object Library (ADO typelib) .
To store data you can follow these steps:
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
公平地说,在 Delphi 上,这些是专门的小部件,而不是普通的 GDI 文本框等,而是带有连接到数据集和表的附加数据库感知层的控件。
.NET也有类似的概念,不知道MFC
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对Doc/View/Frame中数据的抽象是在CDocument中。保存文档时,如果文件没有保存路径,MFC 会提示用户输入文件名,然后在该文件上构造 CArchive 并触发 CDocument::Serialize。您可以将连接字符串存储在文档类中,并使用它在 CDocument::Serialize 中保存数据。
如果您有基于文件的数据库,则集成起来会更容易。重写 CDocument::OnNewDocument 为文档创建一个新的基于文件的数据库,并且
重写 CDocument::OnOpenDocument 以从现有数据库中读取。如果您没有基于文件的数据库,则可以使用 CDocument::SaveModified 覆盖来抑制文件对话框,该覆盖保存数据并清除已修改标志。
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.