如何从列表<class> </class>获取值

时间:2012-07-04 06:37:04

标签: c# class

我有这些类,我想用它来登录,检查电子邮件和密码是否相同,然后它将重定向到相应的页面。

public class Account
  {
    public Account(){}
    public int accID { get; set; }
    public string emailAddress { get; set; }
    public string password { get; set; }
    public string name { get; set; }
    public string company { get; set; }
    public string position { get; set; }
    public string department { get; set; }
    public string mobileNo { get; set; }
    public string officeNo { get; set; }
   }
 public static SADataReader DoSelectQuery(String sql)
    {
        SAConnection myConnection = new SAConnection(DB_STR);
        //open the connection 
        myConnection.Open();
        //Create a command object. 
        SACommand myCommand = myConnection.CreateCommand();

        //Specify a query. 
        myCommand.CommandText = sql;

        //Create a DataReader for the command 
        SADataReader reader = myCommand.ExecuteReader();

        return reader;
    }
 public static List<Account> getAllAccountFromReader(SADataReader reader){
        List<Account> results = new List<Account>();

        while (reader.Read())
        {
            int accID = reader.GetInt32(0);
            string emailAddress = reader.GetString(1);
            string password = reader.GetString(2);
            string name = reader.GetString(3);
            string company = reader.GetString(4);
            string position = reader.GetString(5);
            string department = reader.GetString(6);
            string mobileNo = reader.GetString(7);
            string officeNo = reader.GetString(8);


            Account Accounts = new Account();
            Accounts.accID = accID;
            Accounts.emailAddress = emailAddress;
            Accounts.password = password;
            Accounts.name = name;
            Accounts.company = company;
            Accounts.position = position;
            Accounts.department = department;
            Accounts.mobileNo = mobileNo;
            Accounts.officeNo = officeNo;
            results.Add(Accounts);
        }
        return results;
    }
 public static List<Account> getAllAccounts()
    {
        //Specify a query. 
        string sql = "SELECT accountID,emailAddress,password,name,company,position,department,mobileNo,officeNo FROM account";

        SADataReader reader = DoSelectQuery(sql);
        List<Account> results = getAllAccountFromReader(reader);
        return results;
  }

.CS文件检查字段

protected void btnSubmit_Click(object sender, EventArgs e)
    {
        string email = tbEmail.Text;
        string password = tbPW.Text;
        List<Account> getAccounts = MinuteDB.getAllAccounts();

       // Session["getAllAccount"] = getAccounts;

      if(email ==?? && password == ??)
            {

                       //Session["name"] = name.ToString();
                       //Session["ID"] = Convert.ToInt32(accID.ToString());
                      Response.Redirect("HomePage.aspx");
            }

            else if (email == "" && password == "")
            {
                ScriptManager.RegisterStartupScript(this, GetType(), "error", "alert('Please enter Login and Password!');", true);
            }
            else
            {
                ScriptManager.RegisterStartupScript(this, GetType(), "error", "alert('Wrong Login Or Password!');", true);
            }

    }

如何从List getAccounts中检索电子邮件和密码,以便我可以检查是否(来自列表帐户的电子邮件== 电子邮件&amp;&amp; password == 密码来自列表帐户)??

2 个答案:

答案 0 :(得分:0)

您是否要在帐户列表中找到该电子邮件并检查输入的密码是否匹配?如果是这样,表面上你只需沿着以下几行循环:

private bool isPasswordValid(string email, string password)
{
  foreach (Account account in Accounts)
  {
    if (account.emailAddress != email)
      continue;
    return (account.password == password);
  }
  return false;
}

您也可以返回Dictionary<string, Account>来简化和加快搜索速度。

<强>更新

所以不是以下行:

  if(email ==?? && password == ??)

插入

 if (isPasswordValid(email, password))
   // it is valid
 else
   // it is not valid, redirect

这假设isPasswordValid可以访问getAccounts变量。在您当前的代码中,它将不可见,因此您可能希望将其作为参数传递。

答案 1 :(得分:0)

尝试使用LINQ /扩展方法。

var account = MinuteDB.getAllAccounts()
               .Where(p=> p.emailAddress==email && p.password==password)
               .FirstOrDefault();


if(account!=null)
{
  Session["id"]=account.accID;
  Session["name"]=account.name;
  Response.Redirect("~/other_page.aspx");
}

other_page.aspx中编写以下代码以读取会话密钥值。

int id=0;
string name="";
if(Session["id"]!=null)
   id=int.Parse(Session["id"].ToString());
if(Session["name"]!=null)
   name=Session["name"];

PS:请勿在{{1​​}}中存储密码。您可以将List<T>对象引用分配给会话。

e.g

Account

并从会话中检索 if(account!=null) { Session["account"]=account; Response.Redirect("~/other_page.aspx"); } 值:

account