This is my first time connecting to a database, but i'm having some problems
using Npgsql;
namespace DBPrj
{
class Program
{
static void Main(string[] args)
{
bool boolfound=false;
NpgsqlConnection conn = new NpgsqlConnection("Server=<ip>; Port=5432; User Id=Admin; Password=postgres.1; Database=Test1"); //<ip> is an actual ip address
conn.Open();
NpgsqlCommand cmd = new NpgsqlCommand();
NpgsqlDataReader dr= cmd.ExecuteReader(); //I get InvalidOperationException : The connection is not open.
if (dr.Read())
{
boolfound=true;
Console.WriteLine("connection established");
}
if(boolfound==false)
{
Console.WriteLine("Data does not exist");
}
dr.Close();
conn.Close();
}
}
}
What could be the problem? Is the NpgsqlConnection string written correctly? Could the database be protected from remote access?
How could I fix this problem?
Thanks in advance!