1

I'm having issues with the SQL reader class.

I have a custom object named Airport on the database. But im having trouble using the datareader correctly. When i try to extract all Airports into a list(see method 2 below) it seems to jump of the while(_reader.Read()) loop before adding the object to the list.

Any suggestions?

To extract the object do i use 3 methods:

  1. To find a specific object:

    public Airport FindAirportByCode(string _airportCode)
    {
        con.Open();
        string query = "SELECT * from Airport WHERE airportCode = '" + _airportCode + "'";
        SqlCommand cmd = new SqlCommand(query, con);
        SqlDataReader _reader = cmd.ExecuteReader();
    
        Airport retAirport = BuildAirport(_reader);
        _reader.Close();
        con.Close();
        return retAirport;
    }
    
  2. To get all Airports into a list

    public List<Airport> SelectAll()
    {
        con.Open();
        List<Airport> airports = new List<Airport>();
        string query = "SELECT * from Airport";
        SqlCommand cmd = new SqlCommand(query, con);
    
        SqlDataReader _reader = cmd.ExecuteReader();
    
        while (_reader.Read())
        {
            Airport temAirport = new Airport();
            temAirport = BuildAirport(_reader);
            airports.Add(temAirport); // It seems to skip this step and only add the last Airport from BuildAirport to the list.
        }
    
        _reader.Close();
        con.Close();
        return airports;
    }
    
  3. To build it into an object in C#

        private Airport BuildAirport(SqlDataReader _reader)
    {
    
        Airport temAirport = new Airport();
        while (_reader.Read())
        {
            temAirport.airportCode = (string) _reader["airportCode"];
            temAirport.airportName = (string) _reader["airportName"];
            temAirport.country = (string) _reader["country"];
            temAirport.city = (string) _reader["city"];
        }
        _reader.Close();
    
    
    
        return temAirport;
    }
    
1
  • your select query with parameter is prone to sql injection attack..Use SqlParameter Commented Nov 7, 2013 at 14:26

1 Answer 1

1

In third step you are enumarating reader again and that is why you get only one airport when you want to retrieve all of them.

private Airport BuildAirport(SqlDataReader _reader)
{

    Airport temAirport = new Airport();
    temAirport.airportCode = (string) _reader["airportCode"];
    temAirport.airportName = (string) _reader["airportName"];
    temAirport.country = (string) _reader["country"];
    temAirport.city = (string) _reader["city"];
    return temAirport;
}

And now we have to change your first method because changing BuildAirport breaks it. We have to read one row in FindAirportByCode now.

public Airport FindAirportByCode(string _airportCode)
{
    con.Open();
    string query = "SELECT * from Airport WHERE airportCode = @airportCode";
    SqlCommand cmd = new SqlCommand(query, con);
    cmd.Parameters.AddWithValue("@airportCode", _airportCode);

    SqlDataReader _reader = cmd.ExecuteReader();

    Airport retAirport = null;
    if (_reader.Read())
    {
         retAirport = BuildAirport(_reader);
    }

    _reader.Close();
    con.Close();
    return retAirport;
}

Use parametrized queries for security.

Sign up to request clarification or add additional context in comments.

3 Comments

Why pass the reader when you could pass only the current row?
Could you it explain it a little bit mate ?
@SimonNordahl you were traversing _reader in both second and third method. That resulted in only one airport added to list.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.