Welcome   |   ASP.NET   |   Web Services   |   How Do I...?   |   Class Browser
  |   I want my samples in...   

How Do I...? Common Tasks QuickStart Tutorial

Go To...

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";
C#


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

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


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.

C# TypedData.aspx
View Source

[This sample can be found at M:\web\users\Sites\AspnetQuickStart\v2.0\QuickStart\howto\samples\adoplus\TypedData\
To build this sample, open the SDK command prompt and navigate to the above path. Build the sample using the build tool msbuild passing the solution file as the first parameter: msbuild mySample.sln. The compiled executable will be found in the sub directory \bin directory.]






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


Hosted by MaximumASP | Found a broken link? | Contact Us | Terms and conditions | Privacy Policy | Advertise with us
� 2000 - 2008  Mindcracker LLC. All Rights Reserved