简体   繁体   English

使用for循环在DATAROW中传递值

[英]using for loop to pass values in DATAROW

I've defined a method for UserLogin. 我已经为UserLogin定义了一个方法。 Here I am passing values to DATAROW. 在这里,我将值传递给DATAROW。 Here is the code: 这是代码:

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;

        }
    }

How can I do the below operation using FOR loop , is it possible or not? 如何使用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");

You need to declare below code under one entity 您需要在一个实体下声明以下代码

   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; }

Then in your class make list or collection of entity and add to that list or collection for available rows. 然后在您的类中创建实体的列表或集合,然后将其添加到该列表或集合中以获得可用的行。

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;
            }

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

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