标题:从某数据表中的多个字段提取数据赋值为泛型List的方案???
取消只看楼主
foshan
Rank: 1
等 级:新手上路
威 望:2
帖 子:605
专家分:0
注 册:2006-3-1
结帖率:100%
 问题点数:0 回复次数:0 
从某数据表中的多个字段提取数据赋值为泛型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;
    }
搜索更多相关主题的帖子: 赋值 字段 List 泛型 数据 
2008-01-11 12:39



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-197014-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.017119 second(s), 8 queries.
Copyright©2004-2025, BCCN.NET, All Rights Reserved