列表,用户和地址类的Datareader,这是一个好方法吗? [英] Datareader to list, user and Addresses class, is this a good approach?

查看:85
本文介绍了列表,用户和地址类的Datareader,这是一个好方法吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想用他们的地址写出所有用户的列表,然后编写html。我需要你的意见



I want to write a list of all users with their addresses and then compose html. I need your opinion

public class User
{
    public int ID { get; set; }
    public string Name { get; set; }
    public string Surname { get; set; }
    public List<Address> Addresses { get; set; }
}

public class Address
{
    public int ID { get; set; }
    public string Name { get; set; }
}







public class UserData
{
    public static List<User> GetList()
    {
        List<User> lst = new List<User>();

        using (SqlConnection conn = new SqlConnection(Data.GetConn()))
        {
            using (SqlCommand cmd = conn.CreateCommand())
            {
                if (conn.State == ConnectionState.Closed)
                    conn.Open();

                cmd.CommandText = "select ID,Name,Surname from Users";                

                using (SqlDataReader rdr = cmd.ExecuteReader())
                {
                    while (rdr.Read())
                    {                       
                        User item = new User() {
                            ID = Convert.ToInt32(rdr["ID"]),                            
                            Name = rdr["Name"].ToString(),
                            Surname = rdr["Surname"].ToString(),                                                       
                        };

                        lst.Add(item);
                    }
                }
            }
        }

        return lst;
    }
    
    public static List<Address> GetListA(int UserID)
    {
        List<Address> lst = new List<Address>();

        using (SqlConnection conn = new SqlConnection(Data.GetConn()))
        {
            using (SqlCommand cmd = conn.CreateCommand())
            {
                if (conn.State == ConnectionState.Closed)
                    conn.Open();
               
                cmd.CommandText = "select ID,Name from Addresses where UserID=@UserID";      
                cmd.Parameters.AddWithValue("@UserID", UserID);          
                               
                using (SqlDataReader rdr = cmd.ExecuteReader())
                {
                    while (rdr.Read())
                    {
                        Address item = new Address()
                        {
                            ID = Convert.ToInt32(rdr["ID"]),
                            Name = rdr["Name"].ToString(),
                        };

                        lst.Add(item);
                    }
                }
            }
        }

        return lst;
    }
}







protected void Page_Load(object sender, EventArgs e)
{
    List<User> user = new List<User>();

    user = UserData.GetList();

    foreach (User itemU in user)
    {
        Response.Write(itemU.Name);

        itemU.Addresses = UserData.GetListA(itemU.ID);

        foreach (Address itemA in itemU.Addresses)
        {
            Response.Write(itemA.Name);
        }
    }
}

推荐答案

正确,你应该使用一些现有的asp.net数据控制而不是这样做。



此外,更好的做法是将数据放入块中,而不是使用datareader逐行获取。只需获取数据并将其放入DataSet,然后将数据控件与检索到的数据绑定。只有在发生任何更新或删除时才刷新数据。



如果有帮助请告诉我。





这也取决于要求,所以请在这里查看差异并根据您的方便决定。



点击此处 [ ^ ]
Correct, you should use some existing asp.net data controls rather than doing this.

Also it is better practice to get data in a chunk rather than getting row by row using datareader. Just get the data and have it in DataSet then bind the data control with the data retrieved. Only refresh data when any update or delete is happening.

Please let me know if this helps.


It also depends on the requirement, so please go through the difference here and decide as per your convenience.

Click Here[^]


这篇关于列表,用户和地址类的Datareader,这是一个好方法吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆