When using a database and performing data entry,
records are usually automatically saved as the user moves on, although
this functionality must be implemented by the database developer. If you
want, you can save the records of a table in an XML file. This is easily
done by calling the DataSet::WriteXml() method. |
Practical
Learning: Creating a Data Source
|
|
- Start Microsoft Visual Studio .Net and create a new Windows Forms
Application named ExerciseXML1
- From the Server Explorer, expand the Servers node, down to the pubs
database
- Drag the stores table and drop
it on the form
- Right- click the sqlDataAdapter1 icon in the lower section of the
form and click Generate Dataset...
- Click the text box to the right of the New radio button and change
it to dsStores
- Click OK
- On the Windows Forms section of the Toolbox, click DataGrid and
click the form
- Position the DataGrid to the top-left corner and enlarge it
- Right-click the DataGrid and click Auto Format
- In the Formats list, click Professional 1
- Click OK
- On the form, click the DataGrid to make it is selected. In the
Properties windows, change the following properties:
CaptionText: Book Publishing - Stores
DataSource: dsStores1.stores
Anchor: Top, Bottom, Left, Right
- Add a button to the bottom-left section of the form. Set its Name
to btnLoad and its Caption to Load
- Add another button to the bottom-middle section of the form. Set its
Name to btnSaveAsXML and its Caption to Save As XML
- Add a button to the bottom-right section of the form. Set its Name
to btnClose and its Caption to Close
- Set the Anchor of all three buttons to Bottom only
- Double-click the Load, the Save As XML, and the Close buttons to generate
their events
- Implement them as follows:
System::Void btnLoad_Click(System::Object * sender,
System::EventArgs * e)
{
this->sqlDataAdapter1->Fill(this->dsStores1);
}
System::Void btnSaveAsXML_Click(System::Object * sender,
System::EventArgs * e)
{
this->dsStores1->WriteXml(S"stores.xml");
}
System::Void btnClose_Click(System::Object * sender,
System::EventArgs * e)
{
Close();
}
|
- Press Ctrl + F5 to execute the application and accept to save it:
- Click Load
- When the records are displaying, click Save As XML
- After viewing the form, close it
- Locate the folder where the project was created and double-click the
stores.xml file
|
|
|