I've used PostgreSQL/NpgSQL to connect a c# desktop application to a PostgreSQL database. The first task was to return all table names in my database which works fine. According to this result, I wanted to get all column names per table. But the problem is NpgDatareader always have one column which is the first column of the table and it doesn't contain other column names. Any hint or help would be appreciated.
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++;
}
should be replaced with:
while (reader2.Read())
{
colTitles.Add(reader2[0].ToString());
}
i
was your column index - but you had only one column (you have multiple rows but only one column). So it should always be 0.