c# - 类初始化循环
问题描述
以下代码:
public IEnumerable<AccountStructModel> Query(string query)
{
List<AccountStructModel> output = new List<AccountStructModel>();
using (SqlConnection connection = new SqlConnection("MyConnectionString"))
{
connection.Open();
using (SqlCommand command = new SqlCommand())
{
command.CommandText = query;
command.CommandType = CommandType.Text;
command.Connection = connection;
SqlDataReader reader = command.ExecuteReader();
while (reader.Read())
{
for (int x = 0; x < reader.FieldCount; x++)
{
output.Add(new AccountStructModel { Username = reader[1].ToString(), Password = reader[2].ToString() });
}
}
command.Dispose();
}
connection.Close();
connection.Dispose();
}
return output;
}
给出以下输出:
Username | Password
---------|---------
Tim | randomPassword
问题从这里开始:
new AccountStructModel { Username = reader[1].ToString(), Password = reader[2].ToString() }
我似乎无法找到一种方法来实现一个可以处理以下问题的循环:reader[i]
而不是reader[1]
, reader[2]
, reader[3]
, reader[4]
,也许答案是显而易见的,但我似乎无法那么容易地找到它。
解决方案
推荐阅读
- php - Laravel Autoprefixer 在 webpack 中不起作用
- firebase - 如何部署使用历史模式进行路由的生产 VueJS 项目?
- apache-nifi - 无法使用 InvokeHTTP 上传文件
- jvm - 有没有办法在 JVM 堆栈上交换长(或双)和参考值?
- firebase - 用户设置的 Firebase 函数计划
- python - Merging cropped images using non max suppression
- webserver - How to send data from Arduino to Raspberry pi using ESP8266 Module
- c# - 是否有一种 LINQ 方式可以将相同的字符 n 次附加到字符串中?
- c# - c# 从 GNU objcopy 进程中捕获原始数据,然后转储到文件中
- javascript - Kendo UI 网格 - 更改选定的行并通过箭头键展开/折叠组