我正在开发一个web API与c#和SQL服务器。如果我想在while循环中访问数据(例如:objtoken.customer_id
),以便我可以将其用于进一步开发,我该怎么做?
While循环
while (_Reader.Read())
{
Login_Model objtoken = new Login_Model();
objtoken.user_id = _Reader["user_id"].ToString();
objtoken.customer_id = _Reader["customer_id"].ToString();
_return.Add(objtoken);
}
如果您想访问第一个customer_id,请将其存储在循环外的变量中:
string first_customer_id = null;
while (_Reader.Read())
{
// your original loop content
if (first_customer_id == null)
first_customer_id = objtoken.customer_id;
}
// Here first_customer_id holds the first customer id, or is null if there were no customers