我有这个:
countReader = command7.ExecuteReader();
while (countReader.Read())
{
string countName = countReader["count(*)"].ToString();
}
如何在循环中获取字符串countName?
答案 0 :(得分:1)
如果你想访问while循环之外的变量,你应该像这样在
之外声明它countReader = command7.ExecuteReader();
string countName = String.Empty;
while (countReader.Read())
{
countName = countReader["count(*)"].ToString();
}
答案 1 :(得分:1)
您可以在外部范围内声明它:
countReader = command7.ExecuteReader();
string countName = "";
while (countReader.Read())
{
countName = countReader["count(*)"].ToString();
}
// you can use countName here
请注意,因为您在每次迭代时都会覆盖它的值,所以在循环外部,您将从上一次迭代中获取其值,如果循环未执行,则将获得空字符串。
答案 2 :(得分:0)
string countName;
countReader = command7.ExecuteReader();
while (countReader.Read())
{
countName = countReader["count(*)"].ToString();
}
范围将意味着在退出循环后仍可访问。