在4个结果之后插入字符串
本文关键字:插入 字符串 之后 结果 4个 | 更新日期: 2023-09-27 18:09:26
我使用以下代码从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个结果之后插入字符串"Content"的值在while循环中,例如:
- 名称1 #
- 名称2 #
- 名称3 #
- 名称4 # 测试值
- 名称5 #
使用索引变量:
var index = 0;
while(myReader.Read())
{
Console.WriteLine("- " + myReader["name"].ToString());
if (++index == 4) {
Console.WriteLine("Test value");
}
}
try this
string content = "Test value"
try
{
var counter = 1;
SqlDataReader myReader = null;
SqlCommand myCommand = new SqlCommand("select name from Persons",
myConnection);
myReader = myCommand.ExecuteReader();
while (myReader.Read())
{
if (counter == 4)
{
counter = 1;
Console.WriteLine(content);
}
Console.WriteLine("- " + myReader["name"].ToString());
counter++;
}
}
catch (Exception e)
{
Console.WriteLine(e.ToString());
}
Try This:
int index = 0;
while (myReader.Read()) {
if ((index != 4)) {
index++;
}
else {
// Intert your Code for the Content String Here
}
}