Wrox Programmer Forums
Go Back   Wrox Programmer Forums > ASP.NET and ASP > ASP.NET 2.0 > ASP.NET 2.0 Basics
|
ASP.NET 2.0 Basics If you are new to ASP or ASP.NET programming with version 2.0, this is the forum to begin asking questions. Please also see the Visual Web Developer 2005 forum.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the ASP.NET 2.0 Basics section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old April 15th, 2009, 08:24 PM
Registered User
 
Join Date: Oct 2008
Posts: 5
Thanks: 3
Thanked 0 Times in 0 Posts
Default Use of SQL DataReader in ForEach loop

hello everyone!!!

my problem is that,i hav extracted a whole column from a table through a SQL Command and stored it into a reader object.now i want to check each value of the column against a string.

Can i do it by using ForEach loop?if yes than plz tell me how. if not,than any other way to do this check???

thanks!!!
 
Old April 16th, 2009, 12:54 AM
Lee Dumond's Avatar
Wrox Author
 
Join Date: Jan 2008
Posts: 923
Thanks: 12
Thanked 166 Times in 162 Posts
Default

To loop through a reader, you would generally use a while loop,

Code:
 
while (reader.Read())
{
   if (reader["MyColumn"].ToString() == stringToCompare)
   {
      // do something
   }
}
Remember, a reader provides forward-only access to its values. The Read() method evalutes to true if there are more records left to read, otherwise false. So once reader.Read() becomes false, the while loop would be terminated.
__________________
Visit my blog at http://leedumond.com
Follow me on Twitter: http://twitter.com/LeeDumond

Code:
if (this.PostHelpedYou)
{
   ClickThanksButton(); 
}
The Following User Says Thank You to Lee Dumond For This Useful Post:
bond007 (April 17th, 2009)
 
Old April 22nd, 2009, 06:05 AM
Corin Louis
Guest
 
Posts: n/a
Default

In C#, a DataSet uses a DataReader to populate itself. By definition, a DataReader is basic and efficient access method to access data and return result as they become available. A DataSet, on the other hand, has to wait for the entire query to process.
The Advantages of the DataReader Example of code:
SqlConnection sql= new SqlConnection(connectionString);
SqlDataAdapter a = new SqlDataAdapter
("select * from mytable;",sql);
DataSet ds = new DataSet();
a.Fill(ds);
foreach (DataRow dr in ds.Tables[0].Rows)
{
Console.WriteLine(dr[0].ToString());
}
The foreach loop does not begin until the DataSet is populated. This means any conditional checks or manipulations must occur in the query or after the DataSet is filled.


web developers atlanta





Similar Threads
Thread Thread Starter Forum Replies Last Post
Does foreach loop support datareader deb_kareng C# 1 February 23rd, 2007 05:30 AM
ForEach Loop Bushido121 BOOK: Professional SQL Server 2005 Integration Services ISBN: 0-7645-8435-9 0 October 23rd, 2006 07:55 PM
Getting Error in foreach loop for DataSet bravipandey ADO.NET 2 September 28th, 2005 06:43 PM
Need help with foreach loop Arsi C# 3 September 7th, 2004 09:41 PM
foreach loop not working rajanikrishna Beginning PHP 1 November 3rd, 2003 04:14 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.