• Ivy Seo

Print out data from PostgreSQL table in C#


private static void PrintOutResult()

{

// connect to postgresql

var cs = "Host=HOST.postgres.database.azure.com; Username=USERNAME; Password=PW; Database=DB";


using var con = new NpgsqlConnection(cs);

con.Open();



// PRINT OUT QUERIES

string sql = "SELECT id FROM m7_data_table ORDER BY m7_data_table DESC LIMIT 3";

using var cmd = new NpgsqlCommand(sql, con);


using NpgsqlDataReader rdr = cmd.ExecuteReader();


while (rdr.Read())

{

Console.WriteLine("{0}", rdr.GetInt32(0));


}

Console.WriteLine("Successfully printed out");


}


#C #PostgreSQL