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()

4 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...

This is a very interesting site. The content is very informative and I am so glad that I dropped by. Thanks!

______________________________
Virginia Beach Roofing

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.