ONDotNet.com    
 Published on ONDotNet.com (http://www.ondotnet.com/)
 See this if you're having trouble printing code examples


Migrating ADO.NET Code

by James Culshaw
08/13/2001

With the release of the new .NET Visual Studio (or VS.NET SDK Beta 2), Microsoft has made some fundamental changes. One area that has been changed is ADO.NET. The purpose of this article is to illustrate some of these changes and allow you to move your ADO code from Beta 1 to Beta 2 as simply as possible. All examples in this article are in C#.

Namespace

The first change that Microsoft has made is to the namespace that you import. In Beta 1 you would use the following:

IMPORT namespace="System.Data.ADO";

In Beta 2 you now use the following:

IMPORT namespace="System.Data.OleDB";

Class Names

Coupled with the namespace change, the class names have changes as well. The following table lists some of the Beta 1 class names and their new Beta 2 class names:


Beta 1 Beta 2
ADOCommand OleDbCommand
ADOConnection OleDbConnection
ADODataReader OleDbDataReader

As you can see, the change has been to replace the leading ADO with OleDb (the case is important).

Class Changes

OleDbCommand
The OleDbCommand object no longer has the Execute method. This has been replaced by the ExecuteReader method.

In Beta 1, to populate a DataReader object from the Command object, you would write the following:

ADODataReader drResults;
cmRunProc.Execute(out drResults);

In Beta 2, you would now issue the following:

OleDbDataReader drResults = cmRunProc.ExecuteReader();

The fundamental change is that you no longer have to pass a DataReader object into the Command object to be populated. The Command object now creates and returns the populated DataReader object.

When invoking the ExecuteReader method, you can now provide a CommandBehavior parameter. One use of this is to close the associated connection when the associated DataReader object is closed.

OleDbDataReader drResults =
cmRunProc.ExecuteReader(CommandBehavior.CloseConnection);

Another change to the Command object is that the ActiveConnection property has been renamed to Connection. To associate a Connection object with a Command object in Beta 1, the syntax was as follows:

cmRunProc.ActiveConnection = cnDB;

In Beta 2, the syntax is now:

cmRunProc.Connection = cnDB;

That's it. You have now learned how to move your ADO code from VS.NET Beta 1 to Beta 2 using C#.

James Culshaw is a certified MCP developer and holds a BSc (Honors).


Return to the .NET DevCenter.

Copyright © 2009 O'Reilly Media, Inc.