DB2DataReader.Read Method
Advances the DB2DataReader to the next record.
- Namespace:
IBM.Data.DB2
- Assembly:
IBM.Data.DB2
(inIBM.Data.DB2.dll
)
Syntax
[Visual Basic]
Public Function Read() As Boolean
[C#]
public bool Read();
[C++]
public: bool Read();
[JScript]
public function Read() : Boolean;
Return value
true
if
there are more rows; otherwise, false
.
Remarks
The default position of the DB2®DataReader is before the first record. Therefore, you must call Read before any data.
You can concurrently read data from multiple DB2DataReader instances that use the same DB2Connection instance. Each DB2DataReader instance must be associated with its own DB2Command instance.
Example
[Visual Basic, C#] The following example creates a DB2Connection , a DB2Command , and a DB2DataReader . The example reads through the data, writing it out to the console. Finally, the example closes the DB2DataReader, then the DB2Connection.
[Visual Basic]
Public Sub ReadMyData(myConnString As String)
Dim mySelectQuery As String = "SELECT ID, NAME FROM STAFF"
Dim myConnection As New DB2Connection(myConnString)
Dim myCommand As New DB2Command(mySelectQuery, myConnection)
myConnection.Open()
Dim myReader As DB2DataReader
myReader = myCommand.ExecuteReader()
' Always call Read before accessing data.
While myReader.Read()
Console.WriteLine(myReader.GetInt16(0).ToString() + ", " _
+ myReader.GetString(1))
End While
' always call Close when done reading.
myReader.Close()
' Close the connection when done with it.
myConnection.Close()
End Sub
[C#]
public void ReadMyData(string myConnString)
{
string mySelectQuery = "SELECT ID, NAME FROM STAFF";
DB2Connection myConnection = new DB2Connection(myConnString);
DB2Command myCommand = new DB2Command(mySelectQuery,myConnection);
myConnection.Open();
DB2DataReader myReader;
myReader = myCommand.ExecuteReader();
// Always call Read before accessing data.
while (myReader.Read()) {
Console.WriteLine(myReader.GetInt16(0) + ", " + myReader.GetString(1));
}
// always call Close when done reading.
myReader.Close();
// Close the connection when done with it.
myConnection.Close();
}