AspAlliance.com LogoASPAlliance: Articles, reviews, and samples for .NET Developers
URL:
http://aspalliance.com/articleViewer.aspx?aId=129&pId=-1
A Practical Comparison of ADO and ADO.NET - Part II
page
by Devarticles.com
Feedback
Average Rating: This article has not yet been rated.
Views (Total / Last 10 Days): 16671/ 29

Working with Recordsets/Rowsets

[Note] In ADO 2.x, we used the term recordset to refer to a collection of records returned from a query. In ADO.NET we use the term rowset. [End Note]

If we wanted to use a recordset object in ADO 2.x to store the results of a query and loop through these results, we could do so by calling its open and movenext methods. We could also check whether there were any records beyond our cursor by checking its eof variable, like this:

objRS.ActiveConnection = objConn
objRS.LockType = 1 'adLockReadOnly
objRS.CursorType = 0 'adOpenForwardOnly

objRS.Open "SELECT au_fname + ' ' + au_lname as name FROM authors"

while not objRS.EOF
Response.Write objRS("name") & "<br>"
objRS.MoveNext
wend

In ADO.NET, the OleDbDataReader and SqlDataReader classes can be used to hold rowsets. Both of these classes cannot be directly instantiated, and can only be created as a result of a call to the ExecuteReader method, like this:

string strQuery = "SELECT au_fname + ' ' + au_lname as name FROM authors";

SqlCommand objCmd = new SqlCommand(strQuery, objConn);
objCmd.CommandType = CommandType.Text;
SqlDataReader objDR = objCmd.ExecuteReader();

while(objDR.Read())
{
Response.Write(objDR["name"] + "<br>");
}

The Read() method of the SqlDataReader in our example above advances the rowsets cursor and gives us access to the next row. The SqlDataReader class has an overloaded indexer to accept either the name or index of the field to return. We could just as easily specify a field index rather that a name, like this:

while(objDR.Read())
{
Response.Write(objDR[0] + "
");
}

The return type from the SqlDataReaders indexer is actually an object, which, in our example above is implicitly converted to a string. If we knew that a field was going to be a specific data type, then we could specify an explicit case using C style casting:

while(objDR.Read())
{
// Perform an explicit case from object to int32
int intAge = (int)objDR[5];
}

[Note] When using both the OleDbDataReader and SqlDataReader classes, field indexes start at 0 and not 1. [End Note]

Calling Stored Procedures

[Note] In ADO 2.x, we used the term recordset to refer to a collection of records returned from a query. In ADO.NET we use the term rowset. [End Note]

If we wanted to use a recordset object in ADO 2.x to store the results of a query and loop through these results, we could do so by calling its open and movenext methods. We could also check whether there were any records beyond our cursor by checking its eof variable, like this:

objRS.ActiveConnection = objConn
objRS.LockType = 1 'adLockReadOnly
objRS.CursorType = 0 'adOpenForwardOnly

objRS.Open "SELECT au_fname + ' ' + au_lname as name FROM authors"

while not objRS.EOF
Response.Write objRS("name") & "<br>"
objRS.MoveNext
wend

In ADO.NET, the OleDbDataReader and SqlDataReader classes can be used to hold rowsets. Both of these classes cannot be directly instantiated, and can only be created as a result of a call to the ExecuteReader method, like this:

string strQuery = "SELECT au_fname + ' ' + au_lname as name FROM authors";

SqlCommand objCmd = new SqlCommand(strQuery, objConn);
objCmd.CommandType = CommandType.Text;
SqlDataReader objDR = objCmd.ExecuteReader();

while(objDR.Read())
{
Response.Write(objDR["name"] + "<br>");
}

The Read() method of the SqlDataReader in our example above advances the rowsets cursor and gives us access to the next row. The SqlDataReader class has an overloaded indexer to accept either the name or index of the field to return. We could just as easily specify a field index rather that a name, like this:

while(objDR.Read())
{
Response.Write(objDR[0] + "
");
}

The return type from the SqlDataReaders indexer is actually an object, which, in our example above is implicitly converted to a string. If we knew that a field was going to be a specific data type, then we could specify an explicit case using C style casting:

while(objDR.Read())
{
// Perform an explicit case from object to int32
int intAge = (int)objDR[5];
}

[Note] When using both the OleDbDataReader and SqlDataReader classes, field indexes start at 0 and not 1. [End Note]

Conclusion

In this article we've taken a detailed, comparative look at ADO and ADO.NET. We've seen how it's possible to accomplish one thing in ADO, and then how we can accomplish that exact same thing with ADO.NET. If you're currently working with ASP/VB and ADO, then hopefully this article has given you some incentive to take a look at ADO.NET in further detail. If you're already using ADO.NET, then hopefully this article has cleared up any unsureness that you have.

As you begin to learn more about ADO.NET, it will become more obvious that its underlying data is represented internally as XML, and this makes for a big change in terms of interoperability and speed. If you'd like to learn more about ADO.NET, you can purchase one of the books shown below.

Click here to read the Part I

This article was contributed by the team at http://www.devarticles.com.devArticles provides ASP, PHP and .NET articles, tutorials, reviews, interviews and FREE eBooks. If you're after some serious programming tutorials then...

Visit
http://www.devarticles.com now...OR, for free eBooks
visit
http://www.devarticles.com/ebooks.php


Product Spotlight
Product Spotlight 

©Copyright 1998-2024 ASPAlliance.com  |  Page Processed at 2024-04-20 4:14:17 AM  AspAlliance Recent Articles RSS Feed
About ASPAlliance | Newsgroups | Advertise | Authors | Email Lists | Feedback | Link To Us | Privacy | Search