简体   繁体   English

使用SQL数据读取器在C#中提取1到许多自定义对象

[英]Using SQL datareader to extract 1 to many custom object in c#

I'm having issues with the SQL reader class. 我在SQL阅读器类上遇到问题。

I have a custom object named Airport on the database. 我在数据库上有一个名为Airport的自定义对象。 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. 当我尝试将所有机场提取到列表中时(请参见下面的方法2),在将对象添加到列表中之前,似乎跳过了while(_reader.Read())循环。

Any suggestions? 有什么建议么?

To extract the object do i use 3 methods: 要提取对象,我是否使用3种方法:

  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# 将其构建为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; } 

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. 第三步,您将再次吸引reader ,这就是为什么要检索所有机场时只能得到一个机场。

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. 现在我们必须更改您的第一种方法,因为更改BuildAirport会破坏它。 We have to read one row in FindAirportByCode now. 我们现在必须在FindAirportByCode读取一行。

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. 使用参数化查询来提高安全性。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM