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)
{
string mySelectQuery = "SELECT DeptNo, DName FROM Test.Dept";
MySqlConnection mySqlConnection = new MySqlConnection(myConnString);
MySqlCommand mySqlCommand = new MySqlCommand(mySelectQuery,mySqlConnection);
mySqlConnection.Open();
MySqlDataReader mySqlReader;
mySqlReader = mySqlCommand.ExecuteReader();
// Always call Read before accessing data.
while (mySqlReader.Read()) {
Console.WriteLine(mySqlReader.GetInt32(0) + ", " + mySqlReader.GetString(1));
}
// always call Close when done reading.
mySqlReader.Close();
// Close the connection when done with it.
mySqlConnection.Close();
} |
|
|---|
Public Sub ReadMyData(myConnString As String)
Dim mySelectQuery As String = "SELECT DeptNo, DName FROM Test.Dept"
Dim mySqlConnection As New MySqlConnection(myConnString)
Dim mySqlCommand As New MySqlCommand(mySelectQuery, mySqlConnection)
mySqlConnection.Open()
Dim mySqlReader As MySqlDataReader
mySqlReader = mySqlCommand.ExecuteReader()
' Always call Read before accessing data.
While mySqlReader.Read()
Console.WriteLine(mySqlReader.GetInt32(0).ToString() + ", " _
+ mySqlReader.GetString(1))
End While
' always call Close when done reading.
mySqlReader.Close()
' Close the connection when done with it.
mySqlConnection.Close()
End Sub |