C# 变量作为for循环中的长度?
我在定义for循环中的长度时遇到问题。 我正在计算SQL数据库表中的行数,并希望这个数字是for循环的长度 这是for循环的代码:C# 变量作为for循环中的长度?,c#,for-loop,C#,For Loop,我在定义for循环中的长度时遇到问题。 我正在计算SQL数据库表中的行数,并希望这个数字是for循环的长度 这是for循环的代码: protected void Page_Load(object sender, EventArgs e) { for (int i = 0; i < A; i++) { System.Web.UI.HtmlControls.HtmlGenericControl createDiv =
protected void Page_Load(object sender, EventArgs e)
{
for (int i = 0; i < A; i++)
{
System.Web.UI.HtmlControls.HtmlGenericControl createDiv =
new System.Web.UI.HtmlControls.HtmlGenericControl("DIV");
createDiv.ID = "createDiv";
this.Controls.Add(createDiv);
}
}
我想通过在for循环中创建div来显示数据库中每艘飞船的信息
有人能帮我定义长度吗
谢谢 不要在for循环的条件内调用A方法-它将在每次迭代中执行
int count = A();
for (int i = 0; i < count; i++)
{
System.Web.UI.HtmlControls.HtmlGenericControl createDiv = ...
}
public int A()
{
...
using ( ...
using ( ...
{
...
return count;
}
}
在这种情况下,只需返回int而不是void:
然后在循环中:
int shipCount = A();
for (int i = 0; i < shipCount; i++) {
//make div
}
有太多的话要说
首先,如果您需要在表AlgSchipInfo中显示元素的信息,只需在datatable或其他类似ORM的内容中显示计数后选择这些数据
从数据库中获取数据表时,将其绑定到repeator或其他组件以列出项目。它将比从头开始更有效
如果你有更具体的问题,欢迎你 将A更改为返回int,让它返回count,并将for循环更改为i
public int A() {
int count = 0;
//do db stuff
return count;
}
int shipCount = A();
for (int i = 0; i < shipCount; i++) {
//make div
}