O'Reilly logo

Sams Teach Yourself ADO.NET in 24 Hours by Paul Bertucci, Jason Lefebvre

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Instantiating the DataReader

The DataReader is very easy to use. To get an instance of the DataReader object, you call the ExecuteReader() of the Command object, rather than using the DataAdapter. The ExecuteReader() returns a new instance of a DataReader object ready to display data starting at the first record returned. The code in Listing 8.2 (VB .NET) and in Listing 8.3 (C#) shows how to get a DataReader object.

Listing 8.2. Getting an Instance of the DataReader in Visual Basic .NET
Dim conn as New SqlConnection("Initial Catalog=Northwind;" + _
                              "Server=(local);UID=sa;PWD=;")
Dim cmd as New SqlCommand("SELECT * FROM Employees", conn)

Dim reader as SqlDataReader

conn.Open()
reader = cmd.ExecuteReader()
Listing 8.3. Getting an Instance of ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required