Reads a forward-only stream of rows from MySQL.
The following example creates a
MySqlConnection, a
MySqlCommand, and a
MySqlDataReader. The example reads through the data, writing it out to the console. Finally, the example closes the
MySqlDataReader, then the
MySqlConnection.
public void ReadMyData(string myConnString) {
MySqlConnection myConnection = new MySqlConnection(myConnString);
MySqlCommand myCommand = (MySqlCommand)myConnection.CreateCommand();
myCommand.CommandText = "SELECT DeptNo, DName, Loc FROM Test.Dept";
myConnection.Open();
MySqlDataReader myReader = myCommand.ExecuteReader();
try {
// Always call Read before accessing data.
while (myReader.Read()) {
Console.WriteLine(myReader.GetInt32(0).ToString() + " " +
myReader.GetString(1) + " " + myReader.GetString(2));
}
}
finally {
// always call Close when done reading.
myReader.Close();
// Close the connection when done with it.
myConnection.Close();
}
}
Public Sub ReadMyData(ByVal myConnString As String)
Dim myConnection As New MySqlConnection(myConnString)
Dim myCommand As MySqlCommand = myConnection.CreateCommand()
myCommand.CommandText = "SELECT DeptNo, DName, Loc FROM Test.Dept"
myConnection.Open()
Dim myReader As MySqlDataReader = myCommand.ExecuteReader()
Try
' Always call Read before accessing data.
While myReader.Read()
Console.WriteLine(String.Concat(myReader.GetInt32(0).ToString(), " ", _
myReader.GetString(1), " ", myReader.GetString(2)))
End While
Finally
' always call Close when done reading.
myReader.Close()
' Close the connection when done with it.
myConnection.Close()
End Try
End Sub