Home   |   QuickStart Welcome   |   ASP.NET   |   Web Services   |   How Do I...?   
  |   I want my samples in...      

How Do I...? Common Tasks QuickStart Tutorial

ADO.NET: Work with Typed Data

ADO classic code provides late-bound access to values within its recordset through weakly typed variables. ADO.NET code enables you to access the data held in the DataSet through a "strongly typed" metaphor. This means you can access tables and columns that are part of the DataSet with user-friendly names and strongly typed variables. Therefore, rather than writing the ADO classic code shown below:

AdoRecordset.Find("CustomerID = 'ALFKI'");
AdoRecordSet.Fields["FirstName"].Value  = "Bob";

with a strongly typed DataSet, you write the following ADO.NET code:

CustomerDataSet.Customers["ALFKI"].CustomerName = "Bob";

Additionally, the strongly typed DataSet provides access to row values as the correct, strongly typed value. With ADO, variant types are used when assigning and retrieving data. If the value that you assigned was of the wrong type, ADO would give you a run-time error. With ADO.NET, if the value is of type integer and you attempt to assign a string, you will get a compile-time error.

Given an XML schema that complies with the XSD standard, you can generate a strongly typed DataSet using the XSD.exe tool provided with the .NET Framework SDK. The syntax for generating a DataSet using this tool is:
xsd.exe /d /l:CS {XSDSchemaFileName.xsd}

The /d directive directs the tool to generate a DataSet; the /l: directive specifies the language to use -- either "C#" or "VB."

The following example uses a strongly typed DataSet called "myCustDS" to load a list of customers from the Northwind database. Once the data is loaded using the Fill method, the code loops through each customer in the "Customers" table using the typed myDataRow object. Note the direct access to the "CustomerID" field, as opposed to accessing it through the Fields collection.

Before running the following sample, you must run "nmake /a" in the "..\typeddata\DllSrc" directory from a console window to create the TypedData.dll file.

VB TypedData.aspx
View Source
[This sample can be found at C:\DevFusion.Data\legacy\quickstart.developerfusion.co.uk\QuickStart\howto\samples\adoplus\TypedData\]

Microsoft .NET Framework SDK QuickStart Tutorials Version 2.0
Copyright � 2004 Microsoft Corporation. All rights reserved.