我试图将行存储到字典列表的列表中。基本上换句话说就是行列表。我代表的每一行都是键/值对(列/值)
的列表public List<List<Dictionary<int, string>>> RowsIn(string databaseName, string tableName)
{
const string localConnectionString = "Data Source=(localdb)\\v11.0;Integrated Security=SSPI;";
CurrentConnection = new SqlConnection(localConnectionString);
var rows = new List<List<Dictionary<int, string>>> { new List<Dictionary<int, string>>()};
using (CurrentConnection)
{
CurrentConnection.Open();
SqlDataReader reader;
using (var cmd2 = new SqlCommand("If Exists (SELECT * FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME = '" + tableName + "') SELECT name FROM master.sys.databases WHERE name = N'" + databaseName + "'", CurrentConnection))
{
using (reader = cmd2.ExecuteReader())
{
if (!reader.HasRows) return rows;
while (reader.Read())
{
for (int i = 0; i < reader.FieldCount; i++)
{
rows.Add(
new List<Dictionary<int, string>>
{
new Dictionary<int, int> {{i, reader.GetValue(i)}}
});
}
}
}
}
}
return new List<List<Dictionary<int, string>>>; // none found just return an empty list of rows
}
我不知道我是否正在努力解决这个问题,但我无法在语法上做到这一点。它抱怨这两行代码:
new Dictionary<int, int> {{i, reader.GetValue(i)}}
- 错误&#34;参数类型对象不能分配给&#34;
和
return new List<List<Dictionary<int, string>>>;
答案 0 :(得分:0)
reader.GetValue(i)
我不会返回int
,而是object
。使用(int)reader.GetValue(i)
投放或我相信有reader.GetIntValue(i)
之类的内容(或创建扩展名)。