从某数据表中的多个字段提取数据赋值为泛型List的方案???
看到一个例子(见下),是将从某数据表中的多个字段提取数据赋值为泛型List的方法,想了解除此之外,还有什么方法实现同样的功能???谢谢!/// <summary>
/// Summary description for MilitaryInstallation
/// </summary>
public class AnAthlete
{
public AnAthlete()
{
}
public AnAthlete(int id, string service, string name, string city, string state, string websiteUrl, double latitude, double longitude)
{
_id = id;
_service = service;
_name = name;
_city = city;
_state = state;
_websiteUrl = websiteUrl;
_latitude = latitude;
_longitude = longitude;
}
private int _id;
public int Id
{
get { return _id; }
set { _id = value; }
}
private string _service;
public string Service
{
get { return _service; }
set { _service = value; }
}
private string _name;
public string Name
{
get { return _name; }
set { _name = value; }
}
private string _city;
public string City
{
get { return _city; }
set { _city = value; }
}
private string _state;
public string State
{
get { return _state; }
set { _state = value; }
}
private string _websiteUrl;
public string WebsiteUrl
{
get { return _websiteUrl; }
set { _websiteUrl = value; }
}
private double _latitude;
public double Latitude
{
get { return _latitude; }
set { _latitude = value; }
}
private double _longitude;
public double Longitude
{
get { return _longitude; }
set { _longitude = value; }
}
}
---------------------------------------------------------------
public List<AnAthlete> GetAthletesByService(string service)
{
SqlConnection cn = new SqlConnection(ConfigurationManager.ConnectionStrings["MyDB"].ConnectionString);
SqlCommand cmd = new SqlCommand(
@"SELECT Id, Weight, Name, City, State, WebsiteUrl, Latitude, Longitude
FROM Athlete
WHERE (Weight = @Weight)", cn);
cmd.Parameters.AddWithValue("Weight", service);
List<AnAthlete> retrievedAthletes = new List<AnAthlete>();
cn.Open();
using (SqlDataReader dr = cmd.ExecuteReader(CommandBehavior.CloseConnection))
{
while (dr.Read())
{
retrievedAthletes.Add(new AnAthlete(dr.GetInt32(0),
dr.GetString(1),
dr.GetString(2),
dr.GetString(3),
dr.GetString(4),
dr.GetString(5),
dr.GetDouble(6),
dr.GetDouble(7)));
}
}
return retrievedAthletes;
}