How do you save data in MFC? - c++

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.

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.

Related

Handling fields of Attachment type in MS Access using ADO

I have some fields in my database that will need to store images ( bitmap, JPG or PNG ) and PDF ( or Excel/RTF/TXT ) files. Browsing through Internet, I have learned that MS Access 2007 ( and newer versions ) have a field of type Attachment that can suit my needs.
However, I am filling database via GUI ( made in C++ and WinAPI ), using ADO. I was not able to find an example of inserting/loading such data into/from database using ADO. Searching here through some similar questions ( VB, Delphi, Python...) I have found out that my approach might not be the best, but seems possible to do.
Being inexperienced, and since this is my first time tackling this type of task, I ask community to help me solve this task. Therefore my question:
How should I handle inserting/loading this type of data ( images, documents...) into/from MS Access 2007 (or higher) database using ADO?
Can you provide small code snippet that illustrates inserting/loading such data into/from database using ADO?
Thank you.
If you are planning to only use the C++ application as the front-end and do not expect users to be opening the database in Access itself then I suggest that you avoid using the Attachment field type. Instead, use a separate child table named [Attachments] with a one-to-many Relationship (foreign key constraint) between the [Attachments] table and the parent table (where you were considering using the Attachment field). Then, save the documents as raw binary data in an OLE Object (long binary) field in the child table.
The Attachment field type offers several advantages for applications that use the Access UI. Support for multiple attachments to a single database record can be as simple as dropping an Attachment control onto an Access form. Attachments can also be accessed from Datasheet view, although all you see there is a "paper clip" icon.
Attachment fields can be manipulated from code, but only by using an ACE DAO Recordset2 object (example here). In order to be able to save multiple attachments per record, the Access Database Engine uses a hidden child table. It is possible to pull some information into a SELECT query using "magic" field name qualifiers (e.g., Field1.FileName) but neither ADO nor ODBC can INSERT or UPDATE Attachment field entries.
Since you will not be using the Access UI for your application
you will not be able to use many of the advantages that an Attachment field has to offer, and
you could still manipulate the Attachment field via ACE DAO from your C++ app, but it would be a nuisance.
The one (possibly) significant advantage that you might miss by not using an Attachment field is that the Access Database Engine automatically compresses files in an Attachment field but raw binary data in an OLE Object field is stored uncompressed. If the files you intend to save were all in compressed formats anyway (e.g., JPEG, .docx, .xlsx) then this would not be an issue. However, if you plan to store a lot of large documents in uncompressed formats (e.g., .txt, .rtf) then file bloat could be a problem. In that case you could have your C++ app automatically compress those documents (perhaps using GZipStream) before saving them and uncompress them on retrieval.

View AIS- Data with QT?

anyone know how i can get the Data from a AIS site and view they in a QT Gui?
Can i save them in a SQL database ?
I would have the data from this site : http://www.aishub.net/
AIS : The Automatic Identification System (AIS) is an automatic tracking system used on ships and by vessel traffic services (VTS) for identifying and locating vessels by electronically exchanging data with other nearby ships, AIS base stations, and satellites. When satellites are used to detect AIS signatures then the term Satellite-AIS (S-AIS) is used. AIS information supplements marine radar, which continues to be the primary method of collision avoidance for water transport.
You should register account at http://www.aishub.net/ and subscribe to get access for their API described here. At desktop side you should build Qt application, that will not frequently then every minute call their API URL with your credentials using QNetworkRequest, retrieve QNetworkReply result output (this will be XML or JSON). And then parse the XML or JSON with Qt build-in classes for processing XML or JSON documents. After parsing you should display processed data in your preferred way, for example, sortable table view, or save in database, whatever.
This is not entirely a coding question.. you should found out how to get the data from the site on your own..
But anyway - there's a XML API on that site
Qt supports XML nicely.. you can store the xml if you want in file or in sqllite database, or just parse it an show it.
You can use some view widget to show the parsed data - something like QListView, QTableView ..

Keping history in Winforms C++

I have an application made in winform using C++ (developed in VS 2010). The GUIs have certain text fields, radio button, check boxes etc. To operate the software one has to fill in these fields/buttons/boxes etc.
There are roughly such 50 different GUIs, having roughly 20 fields in each one.
I want that after the application is closed, and restarted, most recent parameters in this fields automatically fill in, so the user do not need to re-enter all those values again.
What is the easiest and simplest way to achieve this?
This is best solved using MVC pattern where the Model contains the data that was filled. The view contains the way it will be presented(such as in Winforms) And lastly the controller which besides doing business logic will need to do some work (for saving the state).
I prefer using serialization for this. You can serialize the model fields and read them back(fields that are necessary only).
If you don't like serialization you can try writing to INI files that will be easy to modify.
Thirdly you can use a database mdf file to store the state.
But to do all this well you will need to modify the App to use MVC architecture.
You can use the Windows Registry with
WindowsFormName (subkey)
---------> ControlName (subkey)
-----------------> ControlValue (subkey)
When the Form is Closing you save the values for each control in the Registry
When the Form is Loading you read the values
If you are satisfied with a quick and dirty solution you can try this:
Implement a save mechanism:
Iterate to all the form controls (recursively if there are panels or
other containers)
If the control is edit (or other type that holds
data) save the control text together with his name
Store the name,
value pairs (INI file for example)
Implement a load mechanism. This assumes that there are no dependencies between values (If there are automated computed fields do not save them)
Load the values from the storage (if you choose files the file name can be the form name)
iterate to all the form controls and if you find its name in loaded data set the value
Once implemented this approach can be used for all the forms and it will cope with adding/removing controls. However if there are business rules maybe is better to use a MVC approach and serialize the model.

Saving user's data for my application

I was wondering what would be the correct method for saving all user data for an application I am working on. The application is in QT. The user inputs a lot of data into the application and the data will be different for every user. I want the ability for the user to save all the current data to a file that can be user by the loaded by the application again once the user wants to use it again or use it on another computer running the application.
What would be the correct and best way to do this? Do I need to use xml format? And then use the xmlreader for QT? Or do I just need to create my own file format and just use the stream to just read everything in. The data in the file will need to be labeled, because it will need to put the data in certain spots on the gui. And the user has the option to dynamically create boxes and tabs that hold certain information.
If you need any more information, please let me know.
A short example:
I am not only reading gui locations.
But the contents of those. For
instance. The user is able to create
tabs that contain edit text boxes. And
those tabs are associated with items
that are in a list. When the user
clicks on an item in the list the user
will be presented with a whole set of
new tabs. And each tab has some
editing forms. The file will need to
contain what is in the list, what tabs
the user has created under each item
in that list and the contents of each
tab associated with the tab of each
item in the list.
In essense, yes you'll be creating your own file format, but the actual content can just be XML in whatever scheme you need. Then you can use Qt's built-in XML processing capabilities to pull the heavy lifting of parsing the text (I personally prefer the DOM model, so I use QDomDocument as my base point), and you'll just need to worry about parsing things to and from the individual nodes.
The Qt framework has some great XML samples if I remember correctly that helped me get off the ground almost immediately. Hope they help!
Another great solution is to use internal database implementation (QSQL on top of sqlite). Compared to the xml solution, it might be more versatile (update when needed, can use external keys). Qt has some rgeat examples about using it aas well.
In terms of dependencies, XML solution will require you to use xml and xmlpatterns (if you want to validate stuff), whereas sqlite solution will require QSQL + sqlite plugin. I think that sqlite guarantees atomicity of writing , thus preventing corruption of data (think : the user is killing the app while it's saving).

Extend ELMAH to include another viewer hitting a different table

I've been doing a little tweaking of the SQL-side of ELMAH to provide basic logging capabilities to my MVC app.
I changed ELMAH_LogError to test the TYPE field for text sent by my App and, if found, inserts to a different table that i'd created by 'Script As'
It was reasonably easy to simply create a new table because I didn't try to rename any fields. The structure is identical to the original. Any chance this same principle (same structure/different table name) could be (easily) extended to providing a new 'Logger.axd' that would provide viewing of my newly created table?
thx