2007-10-25

Retrieving Data Using the DataReader

You can use the ADO.NET DataReader to retrieve a read-only, forward-only stream of data from a database. Results are returned as the query executes, and are stored in the network buffer on the client until you request them using the Read method of the DataReader. Using the DataReader can increase application performance both by retrieving data as soon as it is available, rather than waiting for the entire results of the query to be returned, and (by default) storing only one row at a time in memory, reducing system overhead.

The DataReader class in .NET provides similar functions to SQL Cursors, which are actually not supported in the .NET Framework. DataReades are used to efficiently retrieve a forward-only stream of data from a database. DataReaders are appropriate when the need is to simply display the result set, as only one record at a time is ever present in memory. The DataReader is mainly used in scenarios wherein data need not be updateable nor should persist across multiple requests.

Sample Code (with MsSql)
You must import SqlClient .

Imports System.Data.SqlClient

Dim sConnection As String = "server=(local);uid=sa;pwd=PassWord;database=DatabaseName"

Dim objCommand As New SqlCommand
objCommand.CommandText = "Select * From tablename"
objCommand.Connection = New SqlConnection(sConnection)
objCommand.Connection.Open()

Dim objDataReader As SqlDataReader = objCommand.ExecuteReader()

If objDataReader.HasRows Then
Do While objDataReader.Read()
Console.WriteLine(" Your name is: " & Convert.ToString(objDataReader(0)))
Loop
Else
Console.WriteLine("No rows returned.")
End If

objDataReader.Close()
objCommand.Dispose()

5 comments:

Anonymous said...

Amiable fill someone in on and this mail helped me alot in my college assignement. Thanks you for your information.

Anonymous said...

Thanks for one's marvelous posting! I seriously enjoyed reading it, you may be a great author.I will remember to bookmark your blog and will eventually come back at some point. I want to encourage that you continue your great work, have a nice weekend!

Anonymous said...

thank you Very much , i will apply to Project Herb of Maejo University.

morris said...

it was a wonderful chance to visit this kind of site and I am happy to know. thank you so much for giving us a chance to have this opportunity.. view

cragro.com/파워볼 said...

This Site Eating and Running Verification is a process that checks the capital strength of private This companies and determines whether the refund for rewards can be smooth. Both excellent safety games and sports sites These are the main Toto sites, which have passed the "eat and run" verification. 토토사이트 파워볼사이트 안전놀이터