gpt4 book ai didi

c# - NpgSQL 只返回表的第一列名称

转载 作者:行者123 更新时间:2023-11-29 13:19:15 27 4
gpt4 key购买 nike

我使用 PostgreSQL/NpgSQL 将 c# 桌面应用程序连接到 PostgreSQL 数据库。第一个任务是返回我数据库中的所有表名,这工作正常。根据这个结果,我想获取每个表的所有列名。但问题是 NpgDatareader 总是有一个列是表的第一列,它不包含其他列名。任何提示或帮助将不胜感激。

NpgsqlConnection npgConnection2 = new NpgsqlConnection(connectString);
npgConnection2.Open();
foreach (var t in _tableNames)
{
string sqlColumns = "select column_name from information_schema.columns where table_name='"+ t +"';";
NpgsqlCommand npgCommand2 = new NpgsqlCommand(sqlColumns, npgConnection2);
NpgsqlDataReader reader2 = npgCommand2.ExecuteReader();

List<string> colTitles = new List<string>();
int i = 0;
while (reader2.Read())
{
colTitles.Add(reader2[i].ToString());
i++;
}
_layerObjects.Add(new LayerObject(t.ToString(),colTitles));
}
npgConnection2.Close();

最佳答案

while (reader2.Read())
{
colTitles.Add(reader2[i].ToString());
i++;
}

应该替换为:

while (reader2.Read())
{
colTitles.Add(reader2[0].ToString());
}

i 是您的列索引 - 但您只有一列(您有多个 但只有一列)。所以它应该始终为 0。

关于c# - NpgSQL 只返回表的第一列名称,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44722172/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com