joell
joell

Reputation: 426

.NET Connector only returns first record

I have a weird 'bug/error', at the the .NET Connector only returns to me one record while there are 5 or something. The MySQL Command Line also returns 1. Here is the code I'm really stuck with this, I try to make a Client-Server and let the Client login from a DB at the Server Computer:

Proof

Weird bug

Code:

string strConnect = "server=localhost;user=root;password=xxxx;database=MoopleDEV";
string query = "SELECT name FROM accounts;";

using (MySqlConnection connection = new MySqlConnection(strConnect))
using (MySqlCommand command = new MySqlCommand(query, connection))
using (MySqlDataReader reader = command.ExecuteReader())
{
    connection.Open();

    while (reader.Read())
    {
        Console.WriteLine(reader.GetString("name"));
    }

    connection.Close();
}

Note: It also returns only 'iVision' at the MySQL Command Line Client. Anyone knows what to do? Ps: It doesn't crash or return any exception.

Upvotes: 4

Views: 354

Answers (2)

tgolisch
tgolisch

Reputation: 6734

You are re-opening your connection after creating your reader. Move your connection open statement before you create your command object.

Try this instead:

using (MySqlConnection connection = new MySqlConnection(strConnect))
{
    connection.Open();
    using (MySqlCommand command = new MySqlCommand(query, connection))
    using (MySqlDataReader reader = command.ExecuteReader())
    {

        while (reader.Read())
        {
            Console.WriteLine(reader.GetString("name"));
        }
    }
    connection.Close();
}

Upvotes: 1

MethodMan
MethodMan

Reputation: 18843

IVision I hope that this will give you an idea

why are you using reader.GetString I have seen too many issues with that why not use the (string)reader["name"] approach also when you run the query how many rows are you returning when running the Query Manually also make sure that you are really looking at the right DB database=MoopleDEV could be incorrect or pointed to a different IP Address or location on your Physical Machine. I would also name my local Database Instance to an actual Name as well would cause less confusion

Upvotes: 1

Related Questions