4

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!

2 Answers 2

7

You never assign your NpgsqlConnection to your NpgsqlCommand and you don't supply a query to execute for your NpgsqlDataReader, fixing that should solve the immediate problems.

Also, wrapping at least your NpgsqlConnection in a using()-statement is a good idea to make sure that the connection is always closed, even if there is an exception.

using Npgsql;

namespace DBPrj
{
    class Program
    {
        static void Main(string[] args)
        {
            bool boolfound=false;
            using(NpgsqlConnection conn = new NpgsqlConnection("Server=<ip>; Port=5432; User Id=Admin; Password=postgres.1; Database=Test1"))
            {
                conn.Open();

                NpgsqlCommand cmd = new NpgsqlCommand("SELECT * FROM Table1", conn);
                NpgsqlDataReader dr= cmd.ExecuteReader();
                if (dr.Read())
                {
                    boolfound=true;
                    Console.WriteLine("connection established");
                }
                if(boolfound==false)
                {
                    Console.WriteLine("Data does not exist");
                }
                dr.Close();
            }
        }
    }
}
Sign up to request clarification or add additional context in comments.

1 Comment

my suspicions were correct, about the overloads on the command.
1

In your connection string you may be missing a semi-colon at the end of database.

Database=Test1"

may need to be;

Database=Test1;"

also - it may be worth wrapping your conn.open() in a try catch statement for user-friendliness and ease of catching errors.

Edit 1:

Just did a little reading. Does NpgsqlCommand need parameters to be passed to it? just in pseudo code, something like;

NpgsqlCommand cmd = new NpgsqlCommand(query, conn);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.