Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/postgresql/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# NpgSQL只返回表的第一个列名_C#_Postgresql_Npgsql - Fatal编程技术网

C# NpgSQL只返回表的第一个列名

C# NpgSQL只返回表的第一个列名,c#,postgresql,npgsql,C#,Postgresql,Npgsql,我使用PostgreSQL/NpgSQL将c桌面应用程序连接到PostgreSQL数据库。第一个任务是返回数据库中的所有表名,这很好。根据这个结果,我希望得到每个表的所有列名。但问题是NpgDatareader总是有一列作为表的第一列,并且不包含其他列名。任何提示或帮助都将不胜感激 NpgsqlConnection npgConnection2 = new NpgsqlConnection(connectString); npgConnection2.Open();

我使用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[0].ToString());
}

我是你的列索引-但你只有一列你有多行但只有一列。所以它应该始终为0。

如果删除i++,它是否有效;行吗?@mjwills是的,行得通。在while循环中,reader似乎以reader2[0]的形式返回值,并且不增加i的索引。正如您在注释中提到的!!:谢谢@mjwills。
while (reader2.Read())
{
    colTitles.Add(reader2[0].ToString());
}