使用 for 循环传递 DATAROW 中的值

using for loop to pass values in DATAROW

我已经为 UserLogin 定义了一个方法。在这里,我将值传递给 DATAROW。 这是代码:

public class ApplicationUser
    {        
        public int Id { get; set; }
        public string Username { get; set; }
        public string Password { get; set; }
        public string UserRole { get; set; }            
        public DateTime DateOfRegister { get; set; }

        public bool UserLogin(string Username, string Password)
        {            
            string strQuery = string.Format("select Username, Password from ApplicationUser where Username = '{0}' and Password = '{0}'", Username, Password);            
            DataTable dt = helper.ExecuteDataSet(strQuery).Tables[0];
            if (dt.Rows.Count > 0)
            {
                DataRow ro = dt.Rows[0];
                this.Id = ro.Field<int>("Id");
                this.Username = ro.Field<string>("Username");
                this.Password = ro.Field<string>("Password");                   
                this.UserRole = ro.Field<string>("UserRole");
                return true;
            }
            else
                return false;

        }
    }

如何使用FOR 循环进行以下操作,是否可行?

DataRow ro = dt.Rows[0];    
    this.Id = ro.Field<int>("Id");
    this.Username = ro.Field<string>("Username");
    this.Password = ro.Field<string>("Password");       
    this.UserRole = ro.Field<string>("UserRole");

您需要在一个实体下声明以下代码

   public int Id { get; set; }
    public string Username { get; set; }
    public string Password { get; set; }
    public string UserRole { get; set; }            
    public DateTime DateOfRegister { get; set; }

然后在您的 class 中创建实体列表或集合,并将可用行添加到该列表或集合中。

List<Entity> lstEntity = new List<Entity>();
if (dt.Rows.Count > 0)
            {
            foreach( Datarow r in dt.Rows){ 
                Entity e= new Entity();   
                e.Id = r.Field<int>("Id");
                e.Username = r.Field<string>("Username");
                e.Password = r.Field<string>("Password");                   
                e.UserRole = r.Field<string>("UserRole");
                lstEntity.Add(e);
               }
                return true;
            }