Filling a Database from XML

The same simplicity comes into play when you use an XML file to fill a data table. To see it at work, add another button to Form1 with a name of AddXML and the text set to Add From XML . Double-click the button to edit the handler, and then add this code:

 private: System::Void AddXML_Click(System::Object *  sender,                                    System::EventArgs *  e) {     DataSet* newemployees = new DataSet("NewEmployees");     newemployees->ReadXml("newemployees.xml");     EmployeeData::Employee* emp = new EmployeeData::Employee();     emp->Update(newemployees); } 

This code creates an empty data set, fills it from a file called newemployees.xml, and then uses the data layer to get the new records into the database.

Here is a sample XML file. Enter this XML into a file called newemployees.xml and place the file in the EmployeeUI project folder.

 <?xml version=" 1.0"  encoding=" utf-8" ?> <EmployeeList>   <Employees>     <EmployeeID>301</EmployeeID>     <EmployeeName>Smith, John</EmployeeName>     <EmployeeRate>7.02</EmployeeRate>     <DeptID>MENSCLOTHING</DeptID>   </Employees>   <Employees>     <EmployeeID>302</EmployeeID>     <EmployeeName>Lee, Alice</EmployeeName>     <EmployeeRate>7.55</EmployeeRate>     <DeptID>SPORTING</DeptID>   </Employees> </EmployeeList> 

It's important that none of the EmployeeID values in this XML file are already in use in the database, because an exception is thrown if you try to add a record with a non-unique key. Build and run the application, click the Add From XML button, and then enter a search name of Smith. Click Lookupyou should see that John Smith has been added. Similarly, look up Alice Lee.

Working with XML and data sets is almost ridiculously easy. When you have a powerful data access layer and are using a data grid in your user interface, database applications come together remarkably quickly.

Microsoft Visual C++. NET 2003 Kick Start
Microsoft Visual C++ .NET 2003 Kick Start
ISBN: 0672326000
EAN: 2147483647
Year: 2002
Pages: 141
Authors: Kate Gregory

Similar book on Amazon © 2008-2017.
If you may any questions please contact us: