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

Get Sams Teach Yourself ADO.NET in 24 Hours now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.