0
我已經爲UserLogin定義了一個方法。在這裏,我將價值傳遞給DATAROW。 下面是代碼:使用for循環在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");
@Amit!感謝您的回答。我想知道在這種情況下使用for循環的優點**。使用DATA ROW的最佳方式是什麼?你能解釋我嗎? – user4221591
這取決於你的情況。如果你想要查詢所有返回的行進行進一步處理或傳遞給用戶界面,這是一般的方法,你將通過在UI知道的商業實體或實體中使用循環來積累所有數據,如上所述。根據您的查詢,它將始終返回一行,因爲與用戶ID相關的用戶信息將是唯一的。所以只需使用datarow [0]就足夠了。但是,當你期望不止一行時,在這種情況下,你需要循環或其他數據填充方法。 – Amit
考慮SQL查詢只是從ApplicationUser中選擇用戶名,密碼。在這種情況下,您將獲得多個記錄,並且如果您想要所有用戶信息到datagrid,則要麼綁定返回到網格的數據表,要麼填充您自己的集合並綁定到網格。循環是您可以進行收集的方式之一。 – Amit