我使用以下代码从SQL Server获取结果:
string content = "Test value"
try {
SqlDataReader myReader = null;
SqlCommand myCommand = new SqlCommand("select name from Persons",
myConnection);
myReader = myCommand.ExecuteReader();
while(myReader.Read())
{
Console.WriteLine("- " + myReader["name"].ToString());
}
} catch (Exception e) {
Console.WriteLine(e.ToString());
}
我想要的是插入字符串的值"内容"在4个结果之后的while循环内,例如:
答案 0 :(得分:5)
只需使用索引变量:
var index = 0;
while(myReader.Read())
{
Console.WriteLine("- " + myReader["name"].ToString());
if (++index == 4) {
Console.WriteLine("Test value");
}
}
答案 1 :(得分:1)
试试这个:
int index = 0;
while (myReader.Read()) {
if ((index != 4)) {
index++;
}
else {
// Intert your Code for the Content String Here
}
}