Creating a DataReader, Accessing Data Using a DataReader, Extracting...

Creating a DataReader

The DataReader object is created by calling the Command object's ExecuteReader () method. For example:

// there must be a mySqlCommand object

System.Data. SqlClient.SqlDataReader mySqlReader;

mySqlReader = mySqlCommand.ExecuteReader ();

When you call the ExecuteReader () method, the Command object executes the command that it represents and creates a DataReader of the appropriate type that can be written to a reference variable.

Accessing Data with DataReader

Once you get a reference to the DataReader, you can view the records by loading the data into memory. For a new DataReader, the read pointer is set to the first record of the retrieved sample. To make it accessible, you should call the Read () method. If the record is available, the Read () method takes the DataReader to the next record and returns True, otherwise the Read () method returns false. Then the search of all records of the received sample in the while loop can be performed as follows:

while (myDataReader.Read ())

{

// This code will be executed once // for each record of the retrieved sample.

}

When reading a record using a DataReader, the values ​​of individual fields are accessed through the class indexer, to the elements of which is

go can be accessed by numeric index or by field name, for example:

while (myDataReader.Read ()) {

object myObject = myDataReader [3];

object myOtherObject = myDataReadeif'CustomerlD ];

}

In this case, the DataReader provides all values ​​in the form of elements of type object, although with the help of DataReader it is possible to receive typed data. After executing this loop, you should call the Close () method to close the DataReader, otherwise the DataReader object will keep exclusive access to the active connection, making it unavailable to other objects: myDataReader.Close ();

The following example shows how to retrieve the records of the retrieved record to display the contents of one of the columns in the Console window. This example assumes that there is an OleDbCommand object named myOleDbCommand whose Connection property defines a connection named myConnection:

// Open the active connection, myConnection.Open ();

// Create a DataReader object

System.Data.OleDb.OleDbDataReader myReader;

myReader = myOleDbCommand.ExecuteReader ();

// Call the Read method before attempting to read the data,

while (myReader.Read ()) {

// Get access to the field by name

Console.WriteLine (myReader [ Customers ]. ToString ());

}

myReader.Close (); // close the DataReader myConnection.Close;

// close the connection

Extract typed data with DataReader

Objects of the DataReader class not only provide data of type object, but also allow retrieving other types of data from the received sample using special methods. The names of these methods are formed from the Get prefix and the name of the type of data retrieved. For example, a method that retrieves Boolean values ​​is called GetBoolean (). For example:

string myString = myDataReader.GetBoolean (3);

If you use this method of retrieving data, you need to specify a sequence number, not the field name. If only the field name is known, you can define its serial number by calling the GetOrdinal () method, for example: int CustomerlD; string Customer:

CustomerId = myDataReader.GetOrdinal ("CustomerID");

Customer = myDataReader.GetString (CustomerID);

thematic pictures

Also We Can Offer!

Other services that we offer

If you don’t see the necessary subject, paper type, or topic in our list of available services and examples, don’t worry! We have a number of other academic disciplines to suit the needs of anyone who visits this website looking for help.

How to ...

We made your life easier with putting together a big number of articles and guidelines on how to plan and write different types of assignments (Essay, Research Paper, Dissertation etc)