您好我正在学校为学生构建一个帮助应用程序,使用数据库和信号器来实时更新列表。
connection.Open();
using (var command = new SqlCommand(@"SELECT [Id], [Name] FROM [dbo].[HelpLists]", connection))
{
command.Notification = null;
var dependency = new SqlDependency(command);
dependency.OnChange += new OnChangeEventHandler(dependency_OnChange);
if (connection.State == ConnectionState.Closed)
connection.Open();
var reader = command.ExecuteReader();
while (reader.Read())
{
messages.Add(item: new Messages { Number = (int)reader["Id"], Message = (string)reader["Name"] });
}
}
我想要的代码部分是While loop
让我说我的select语句有3行。然后我想将command
传递给for / foreach loop
有点像:
for (int i = 0; i < reader.FieldCount; i++)
{
messages.Add(item: new Messages { Number = i + 1, Message = (string)reader["Name"] });
}
原因是我需要从1开始的数字,并且对于select语句中的每一行增加1
答案 0 :(得分:1)
您可以尝试此代码。
connection.Open();
using (var command = new SqlCommand(@"SELECT [Id], [Name] FROM [dbo].[HelpLists]", connection))
{
command.Notification = null;
var dependency = new SqlDependency(command);
dependency.OnChange += new OnChangeEventHandler(dependency_OnChange);
if (connection.State == ConnectionState.Closed)
connection.Open();
var reader = command.ExecuteReader();
//my change is here
List<reader > readers = new List<reader>();
for(reader rs:readers )
{
System.out.println(rs.id);
System.out.println(rs.name);
}
}