Lab 6-2: Connecting with an XML Data Store

Lab 6-2: Connecting with an XML Data Store

The ADO .NET data adapters provide you with the tools necessary to interact with all kinds of relational databases. Sometimes, however, you might find it necessary to work with data stored in other formats. In this lab, you will connect to and interact with data stored in an XML file. You will create a typed dataset from a pre-existing schema, and then load data from an XML file into that dataset and browse the data.

Before You Begin

There are no prerequisites to completing this lab.

Estimated lesson time: 45 minutes

Exercise 6-2.1: Accessing the XML Data Store

In this exercise, you will access data stores in an XML file. You will create a typed DataSet from a pre-existing schema definition, build a simple interface to view the data, and load the data from the XML file into the dataset.

To access an XML Data Store

  1. If you haven t already done so, copy the Northwind.xml and Dataset1.xsd files from the Supplemental Course Materials CD-ROM to your hard drive. The Northwind.xml file is an XML representation of the Microsoft Northwind sample database, a sample database included with Microsoft Access and Microsoft SQL Server. The Dataset1.xsd file is a representation of the schema of this database, including the relationships between the data.

  2. With Visual Studio .NET, create a new Windows Forms.

  3. From the Project menu, choose Add Existing Item. In the Add Existing Item dialog box, browse to Dataset1.xsd on your hard drive, and select it to add it to your project.

    NOTE
    Visual C# users must set the file type to All Files in the Type of File dropdown list box.

  4. In Solution Explorer, double-click Dataset1.xsd to open the schema in Schema View. Examine the tables and relations represented by the schema. Note that the schema contains no actual data. Rather, it describes the types and relations of the data contained by the dataset.

  5. In the Schema View, right-click the background and choose Generate Dataset to generate the typed dataset for your application.

  6. In Solution Explorer, right-click Form1 and choose View Designer. The designer for Form1 opens.

  7. From the Toolbox, drag a DataGrid control and two Button controls to the designer surface. Set the properties for the Button controls as shown in Table 6.12.

    Table 6-12. Button Properties

    Button

    Text Property

    Name Property

    Button1

    Load Records

    btnLoad

    Button2

    Save Records

    btnSave

  8. In the designer, double-click Form1 to open the Form1.Load event handler. Outside of the method declaration, add the following line of code:

    Visual Basic .NET

    Dim myDataset As New DataSet1()

    Visual C#

    DataSet1 myDataset = new DataSet1();

  9. Within the Form1.Load event handler, add the following line of code:

    Visual Basic .NET

    DataGrid1.DataSource = myDataset

    Visual C#

    dataGrid1.DataSource = myDataset;

  10. In Solution Explorer, right-click Form1 and choose View Designer to return to the designer. Double-click btnLoad to open the btnLoad.Click event handler and add the following code:

    Visual Basic .NET

    ' In this example, <path> is a string representing the physical path ' to the Northwind.XML file on your hard drive. myDataset.ReadXml(<path>)

    Visual C#

    // In this example, <path> is a string representing the physical path // to the Northwind.XML file on your hard drive. myDataset.ReadXml(<path>);

    NOTE
    In this example, <path> stands for a string that represents the physical path to the Northwind.xml file. Thus, if the Northwind.xml file were located in your root directory, you would replace <path> with C:\Northwind.xml for Visual Basic .NET and C:\\Northwind.xml for Visual C#.

  11. In Solution Explorer, right-click Form1 and choose view designer to return to the designer. Double-click btnSave to open the btnSave.Click event handler and add the following code:

    Visual Basic .NET

    ' In this example, <path> is a string representing the physical path ' to the Northwind.XML file on your hard drive. myDataset.WriteXml(<path>)

    Visual C#

    // In this example, <path> is a string representing the physical path // to the Northwind.XML file on your hard drive. myDataset.WriteXml(<path>);

    NOTE
    As in the previous example, <path> stands for a string that represents the physical path to the Northwind.xml file. Thus, if the Northwind.xml file were located in your root directory, you would replace <path> with C:\Northwind.xml for Visual Basic .NET and C:\\Northwind.xml for Visual C#.

  12. Press F5 to build and test your application. Use the DataGrid to browse the data in the XML file and make changes. Save any changes you make and close the application. Press F5 to restart the application. Note that changes you made have been written to the XML file.



MCAD(s)MCSD Self-Paced Training Kit(c) Developing Windows-Based Applications With Microsoft Visual Basic. Net a[.  .. ]0-316
MCAD(s)MCSD Self-Paced Training Kit(c) Developing Windows-Based Applications With Microsoft Visual Basic. Net a[. .. ]0-316
ISBN: 735619263
EAN: N/A
Year: 2003
Pages: 110

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net